Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Someone
Sunday, October 28th, 2007 at 7:27:19pm UTC 

  1. diff --git a/apps/buffering.c b/apps/buffering.c
  2. index 0fd0c4c..5afb560 100644
  3. --- a/apps/buffering.c
  4. +++ b/apps/buffering.c
  5. @@ -116,6 +116,7 @@ struct memory_handle {
  6.      size_t filerem;            /* Remaining bytes of file NOT in buffer */
  7.      volatile size_t available; /* Available bytes to read from buffer */
  8.      size_t offset;             /* Offset at which we started reading the file */
  9. +    struct mutex mutex;
  10.      struct memory_handle *next;
  11.  };
  12.  /* invariant: filesize == offset + available + filerem */
  13. @@ -272,6 +273,7 @@ static struct memory_handle *add_handle(size_t data_size, const bool can_wrap,
  14.      /* only advance the buffer write index of the size of the struct */
  15.      buf_widx = RINGBUF_ADD(buf_widx, sizeof(struct memory_handle));
  16.  
  17. +    mutex_init(&new_handle->mutex);
  18.      new_handle->id = cur_handle_id;
  19.      /* Use += 2 instead of ++ to guarantee that the low bit is always high and
  20.       * prevent the assignment of a zero id when wrapping. */
  21. @@ -601,6 +603,8 @@ static void reset_handle(int handle_id)
  22.      if (!h)
  23.          return;
  24.  
  25. +    mutex_lock(&h->mutex);
  26. +
  27.      h->widx = h->data;
  28.      if (h == cur_handle)
  29.          buf_widx = h->widx;
  30. @@ -610,6 +614,8 @@ static void reset_handle(int handle_id)
  31.      if (h->fd >= 0) {
  32.          lseek(h->fd, h->offset, SEEK_SET);
  33.      }
  34. +
  35. +    mutex_unlock(&h->mutex);
  36.  }
  37.  
  38.  /* Seek to a nonbuffered part of a handle by rebuffering the data. */
  39. @@ -619,7 +625,11 @@ static void rebuffer_handle(int handle_id, size_t newpos)
  40.      if (!h)
  41.          return;
  42.  
  43. +    DEBUGF("rebuffer_handle\n");
  44. +
  45. +    mutex_lock(&h->mutex);
  46.      h->offset = newpos;
  47. +    mutex_unlock(&h->mutex);
  48.  
  49.      LOGFQUEUE("? >| buffering Q_RESET_HANDLE");
  50.      queue_send(&buffering_queue, Q_RESET_HANDLE, handle_id);
  51. @@ -627,7 +637,9 @@ static void rebuffer_handle(int handle_id, size_t newpos)
  52.      LOGFQUEUE("? >| buffering Q_BUFFER_HANDLE");
  53.      queue_send(&buffering_queue, Q_BUFFER_HANDLE, handle_id);
  54.  
  55. +    mutex_lock(&h->mutex);
  56.      h->ridx = h->data;
  57. +    mutex_unlock(&h->mutex);
  58.  }
  59.  
  60.  static bool close_handle(int handle_id)
  61. @@ -636,11 +648,15 @@ static bool close_handle(int handle_id)
  62.      if (!h)
  63.          return false;
  64.  
  65. +    mutex_lock(&h->mutex);
  66. +
  67.      if (h->fd >= 0) {
  68.          close(h->fd);
  69.          h->fd = -1;
  70.      }
  71.  
  72. +    mutex_unlock(&h->mutex);
  73. +
  74.      rm_handle(h);
  75.      return true;
  76.  }
  77. @@ -651,22 +667,27 @@ static void shrink_handle(int handle_id)
  78.  {
  79.      size_t delta;
  80.      struct memory_handle *h = find_handle(handle_id);
  81. -
  82.      if (!h)
  83.          return;
  84.  
  85. +    mutex_lock(&h->mutex);
  86. +
  87.      if (h->next && (h->type == TYPE_ID3 || h->type == TYPE_CUESHEET ||
  88.                      h->type == TYPE_IMAGE) && h->filerem == 0 )
  89.      {
  90.          /* metadata handle: we can move all of it */
  91. -        size_t handle_distance =
  92. +        size_t handle_distance =
  93.              RINGBUF_SUB((unsigned)((void *)h->next - (void*)buffer), h->data);
  94.          delta = handle_distance - h->available;
  95.  
  96. +        mutex_unlock(&h->mutex);
  97. +
  98.          /* The value of delta might change for alignment reasons */
  99.          h = move_handle(h, &delta, h->available);
  100.          if (!h) return;
  101.  
  102. +        mutex_lock(&h->mutex);
  103. +
  104.          size_t olddata = h->data;
  105.          h->data = RINGBUF_ADD(h->data, delta);
  106.          h->ridx = RINGBUF_ADD(h->ridx, delta);
  107. @@ -683,13 +704,17 @@ static void shrink_handle(int handle_id)
  108.      {
  109.          /* only move the handle struct */
  110.          delta = RINGBUF_SUB(h->ridx, h->data);
  111. +        mutex_unlock(&h->mutex);
  112.          h = move_handle(h, &delta, 0);
  113.          if (!h) return;
  114.  
  115. +        mutex_lock(&h->mutex);
  116.          h->data = RINGBUF_ADD(h->data, delta);
  117.          h->available -= delta;
  118.          h->offset += delta;
  119.      }
  120. +
  121. +    mutex_unlock(&h->mutex);
  122.  }
  123.  
  124.  /* Fill the buffer by buffering as much data as possible for handles that still
  125. @@ -744,6 +769,7 @@ void update_data_counters(void)
  126.  
  127.      m = first_handle;
  128.      while (m) {
  129. +        mutex_lock(&m->mutex);
  130.          data_counters.buffered += m->available;
  131.          data_counters.wasted += RINGBUF_SUB(m->ridx, m->data);
  132.          data_counters.remaining += m->filerem;
  133. @@ -751,6 +777,7 @@ void update_data_counters(void)
  134.          if (m->id >= base_handle_id)
  135.              data_counters.useful += RINGBUF_SUB(m->widx, m->ridx);
  136.  
  137. +        mutex_unlock(&m->mutex);
  138.          m = m->next;
  139.      }
  140.  }
  141. @@ -800,6 +827,8 @@ int bufopen(const char *file, size_t offset, enum data_type type)
  142.          return BUFFER_FULL;
  143.      }
  144.  
  145. +    mutex_lock(&h->mutex);
  146. +
  147.      strncpy(h->path, file, MAX_PATH);
  148.      h->filesize = size;
  149.      h->filerem = size - offset;
  150. @@ -822,6 +851,8 @@ int bufopen(const char *file, size_t offset, enum data_type type)
  151.      }
  152.  
  153.      logf("bufopen: new hdl %d", h->id);
  154. +
  155. +    mutex_unlock(&h->mutex);
  156.      return h->id;
  157.  }
  158.  
  159. @@ -842,6 +873,8 @@ int bufalloc(const void *src, size_t size, enum data_type type)
  160.      if (!h)
  161.          return BUFFER_FULL;
  162.  
  163. +    mutex_lock(&h->mutex);
  164. +
  165.      if (src) {
  166.          if (type == TYPE_ID3 && size == sizeof(struct mp3entry)) {
  167.              /* specially take care of struct mp3entry */
  168. @@ -866,6 +899,8 @@ int bufalloc(const void *src, size_t size, enum data_type type)
  169.      buf_widx += size;  /* safe too */
  170.  
  171.      logf("bufalloc: new hdl %d", h->id);
  172. +
  173. +    mutex_unlock(&h->mutex);
  174.      return h->id;
  175.  }
  176.  
  177. @@ -890,8 +925,11 @@ int bufseek(int handle_id, size_t newpos)
  178.      if (!h)
  179.          return HANDLE_NOT_FOUND;
  180.  
  181. +    mutex_lock(&h->mutex);
  182. +
  183.      if (newpos > h->filesize) {
  184.          /* access beyond the end of the file */
  185. +        mutex_unlock(&h->mutex);
  186.          return INVALID_VALUE;
  187.      }
  188.      else if (newpos < h->offset || h->offset + h->available < newpos) {
  189. @@ -901,6 +939,8 @@ int bufseek(int handle_id, size_t newpos)
  190.      else {
  191.          h->ridx = RINGBUF_ADD(h->data, newpos - h->offset);
  192.      }
  193. +
  194. +    mutex_unlock(&h->mutex);
  195.      return 0;
  196.  }
  197.  
  198. @@ -908,36 +948,51 @@ int bufseek(int handle_id, size_t newpos)
  199.     Return 0 for success and < 0 for failure */
  200.  int bufadvance(int handle_id, off_t offset)
  201.  {
  202. -    const struct memory_handle *h = find_handle(handle_id);
  203. +    int ret;
  204. +    struct memory_handle *h = find_handle(handle_id);
  205.      if (!h)
  206.          return HANDLE_NOT_FOUND;
  207.  
  208. +    mutex_lock(&h->mutex);
  209.      size_t newpos = h->offset + RINGBUF_SUB(h->ridx, h->data) + offset;
  210. -    return bufseek(handle_id, newpos);
  211. +    ret = bufseek(handle_id, newpos);
  212. +    mutex_unlock(&h->mutex);
  213. +    return ret;
  214.  }
  215.  
  216.  /* Copy data from the given handle to the dest buffer.
  217.     Return the number of bytes copied or < 0 for failure. */
  218.  ssize_t bufread(int handle_id, size_t size, void *dest)
  219.  {
  220. -    const struct memory_handle *h = find_handle(handle_id);
  221. +    struct memory_handle *h = find_handle(handle_id);
  222.      if (!h)
  223.          return HANDLE_NOT_FOUND;
  224.  
  225. +    mutex_lock(&h->mutex);
  226. +
  227.      size_t ret;
  228.      size_t copy_n = RINGBUF_SUB(h->widx, h->ridx);
  229.  
  230.      if (size == 0 && h->filerem > 0 && copy_n == 0)
  231. +    {
  232.          /* Data isn't ready */
  233. +        mutex_unlock(&h->mutex);
  234.          return DATA_NOT_READY;
  235. +    }
  236.  
  237.      if (copy_n < size && h->filerem > 0)
  238. +    {
  239.          /* Data isn't ready */
  240. +        mutex_unlock(&h->mutex);
  241.          return DATA_NOT_READY;
  242. +    }
  243.  
  244.      if (copy_n == 0 && h->filerem == 0)
  245. +    {
  246.          /* File is finished reading */
  247. +        mutex_unlock(&h->mutex);
  248.          return 0;
  249. +    }
  250.  
  251.      ret = MIN(size, copy_n);
  252.  
  253. @@ -953,6 +1008,7 @@ ssize_t bufread(int handle_id, size_t size, void *dest)
  254.          memcpy(dest, &buffer[h->ridx], ret);
  255.      }
  256.  
  257. +    mutex_unlock(&h->mutex);
  258.      return ret;
  259.  }
  260.  
  261. @@ -963,24 +1019,35 @@ ssize_t bufread(int handle_id, size_t size, void *dest)
  262.     The guard buffer may be used to provide the requested size */
  263.  ssize_t bufgetdata(int handle_id, size_t size, void **data)
  264.  {
  265. -    const struct memory_handle *h = find_handle(handle_id);
  266. +    struct memory_handle *h = find_handle(handle_id);
  267.      if (!h)
  268.          return HANDLE_NOT_FOUND;
  269.  
  270. +    mutex_lock(&h->mutex);
  271. +
  272.      ssize_t ret;
  273.      size_t copy_n = RINGBUF_SUB(h->widx, h->ridx);
  274.  
  275.      if (size == 0 && h->filerem > 0 && copy_n == 0)
  276. +    {
  277.          /* Data isn't ready */
  278. +        mutex_unlock(&h->mutex);
  279.          return DATA_NOT_READY;
  280. +    }
  281.  
  282.      if (copy_n < size && h->filerem > 0)
  283. +    {
  284.          /* Data isn't ready */
  285. +        mutex_unlock(&h->mutex);
  286.          return DATA_NOT_READY;
  287. +    }
  288.  
  289.      if (copy_n == 0 && h->filerem == 0)
  290. +    {
  291.          /* File is finished reading */
  292. +        mutex_unlock(&h->mutex);
  293.          return 0;
  294. +    }
  295.  
  296.      if (h->ridx + size > buffer_len && copy_n >= size)
  297.      {
  298. @@ -996,6 +1063,7 @@ ssize_t bufgetdata(int handle_id, size_t size, void **data)
  299.      }
  300.  
  301.      *data = &buffer[h->ridx];
  302. +    mutex_unlock(&h->mutex);
  303.      return ret;
  304.  }
  305.  
  306. @@ -1019,19 +1087,28 @@ management functions for all the actual handle management work.
  307.  /* Get a handle offset from a pointer */
  308.  ssize_t buf_get_offset(int handle_id, void *ptr)
  309.  {
  310. -    const struct memory_handle *h = find_handle(handle_id);
  311. +    ssize_t ret;
  312. +    struct memory_handle *h = find_handle(handle_id);
  313.      if (!h)
  314.          return HANDLE_NOT_FOUND;
  315.  
  316. -    return (size_t)ptr - (size_t)&buffer[h->ridx];
  317. +    mutex_lock(&h->mutex);
  318. +    ret = (size_t)ptr - (size_t)&buffer[h->ridx];
  319. +    mutex_unlock(&h->mutex);
  320. +    return ret;
  321.  }
  322.  
  323.  ssize_t buf_handle_offset(int handle_id)
  324.  {
  325. -    const struct memory_handle *h = find_handle(handle_id);
  326. +    ssize_t ret;
  327. +    struct memory_handle *h = find_handle(handle_id);
  328.      if (!h)
  329.          return HANDLE_NOT_FOUND;
  330. -    return h->offset;
  331. +
  332. +    mutex_lock(&h->mutex);
  333. +    ret = h->offset;
  334. +    mutex_unlock(&h->mutex);
  335. +    return ret;
  336.  }
  337.  
  338.  void buf_request_buffer_handle(int handle_id)

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

comments powered by Disqus