- xbt_mutex_lock(queue->mutex);
- if (queue->capacity != 0 && queue->capacity == xbt_dynar_length(queue->data)) {
- DEBUG2("Capacity of %p exceded (=%d). Waiting",queue,queue->capacity);
- xbt_cond_timedwait(queue->not_full,queue->mutex,delay);
- }
- /* check if a timeout occurs */
-
- if (queue->capacity != 0 && queue->capacity == xbt_dynar_length(queue->data)) {
-
- xbt_mutex_unlock(queue->mutex);
- THROW0(timeout_error,0,"Timeout");
- }
- else {
- xbt_dynar_unshift(queue->data,src);
- xbt_cond_signal(queue->not_empty);
- xbt_mutex_unlock(queue->mutex);
- }
+ double begin = xbt_time();
+ xbt_ex_t e;
+
+ xbt_mutex_acquire(queue->mutex);
+
+ if (delay==0) {
+ if (queue->capacity != 0 &&
+ queue->capacity == xbt_dynar_length(queue->data)) {
+
+ xbt_mutex_release(queue->mutex);
+ THROW2(timeout_error,0,"Capacity of %p exceded (=%d), and delay = 0",
+ queue,queue->capacity);
+ }
+ } else {
+ while (queue->capacity != 0 &&
+ queue->capacity == xbt_dynar_length(queue->data) &&
+ (delay<0 || (xbt_time() - begin) <= delay) ) {
+
+ DEBUG2("Capacity of %p exceded (=%d). Waiting",
+ queue,queue->capacity);
+ TRY {
+ xbt_cond_timedwait(queue->not_full,queue->mutex,
+ delay < 0 ? -1 : delay - (xbt_time()-begin));
+ } CATCH(e) {
+ xbt_mutex_release(queue->mutex);
+ RETHROW;
+ }
+ }
+ }
+
+ xbt_dynar_unshift(queue->data,src);
+ xbt_cond_signal(queue->not_empty);
+ xbt_mutex_release(queue->mutex);