Subversion Repositories HelenOS

Rev

Rev 2310 | Rev 2914 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2310 Rev 2909
Line 52... Line 52...
52
#include <time/timeout.h>
52
#include <time/timeout.h>
53
#include <arch.h>
53
#include <arch.h>
54
#include <context.h>
54
#include <context.h>
55
#include <adt/list.h>
55
#include <adt/list.h>
56
 
56
 
57
static void waitq_timeouted_sleep(void *data);
57
static void waitq_sleep_timed_out(void *data);
58
 
58
 
59
/** Initialize wait queue
59
/** Initialize wait queue
60
 *
60
 *
61
 * Initialize wait queue.
61
 * Initialize wait queue.
62
 *
62
 *
Line 79... Line 79...
79
 * overlap. In that case it behaves just as though there was no
79
 * overlap. In that case it behaves just as though there was no
80
 * timeout at all.
80
 * timeout at all.
81
 *
81
 *
82
 * @param data Pointer to the thread that called waitq_sleep_timeout().
82
 * @param data Pointer to the thread that called waitq_sleep_timeout().
83
 */
83
 */
84
void waitq_timeouted_sleep(void *data)
84
void waitq_sleep_timed_out(void *data)
85
{
85
{
86
    thread_t *t = (thread_t *) data;
86
    thread_t *t = (thread_t *) data;
87
    waitq_t *wq;
87
    waitq_t *wq;
88
    bool do_wakeup = false;
88
    bool do_wakeup = false;
89
    DEADLOCK_PROBE_INIT(p_wqlock);
89
    DEADLOCK_PROBE_INIT(p_wqlock);
Line 181... Line 181...
181
 * Sleepers are organised in a FIFO fashion in a structure called wait queue.
181
 * Sleepers are organised in a FIFO fashion in a structure called wait queue.
182
 *
182
 *
183
 * This function is really basic in that other functions as waitq_sleep()
183
 * This function is really basic in that other functions as waitq_sleep()
184
 * and all the *_timeout() functions use it.
184
 * and all the *_timeout() functions use it.
185
 *
185
 *
186
 * @param wq Pointer to wait queue.
186
 * @param wq        Pointer to wait queue.
187
 * @param usec Timeout in microseconds.
187
 * @param usec      Timeout in microseconds.
188
 * @param flags Specify mode of the sleep.
188
 * @param flags     Specify mode of the sleep.
189
 *
189
 *
190
 * The sleep can be interrupted only if the
190
 * The sleep can be interrupted only if the
191
 * SYNCH_FLAGS_INTERRUPTIBLE bit is specified in flags.
191
 * SYNCH_FLAGS_INTERRUPTIBLE bit is specified in flags.
192
 *
192
 *
193
 * If usec is greater than zero, regardless of the value of the
193
 * If usec is greater than zero, regardless of the value of the
Line 198... Line 198...
198
 * the call will not return until wakeup or interruption comes.
198
 * the call will not return until wakeup or interruption comes.
199
 *
199
 *
200
 * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is set in flags, the
200
 * If usec is zero and the SYNCH_FLAGS_NON_BLOCKING bit is set in flags, the
201
 * call will immediately return, reporting either success or failure.
201
 * call will immediately return, reporting either success or failure.
202
 *
202
 *
203
 * @return One of: ESYNCH_WOULD_BLOCK, ESYNCH_TIMEOUT, ESYNCH_INTERRUPTED,
203
 * @return      Returns one of ESYNCH_WOULD_BLOCK, ESYNCH_TIMEOUT,
204
 * ESYNCH_OK_ATOMIC, ESYNCH_OK_BLOCKED.
204
 *          ESYNCH_INTERRUPTED, ESYNCH_OK_ATOMIC and
-
 
205
 *          ESYNCH_OK_BLOCKED.
205
 *
206
 *
206
 * @li ESYNCH_WOULD_BLOCK means that the sleep failed because at the time of the
207
 * @li  ESYNCH_WOULD_BLOCK means that the sleep failed because at the time of
207
 * call there was no pending wakeup.
208
 *  the call there was no pending wakeup.
208
 *
209
 *
209
 * @li ESYNCH_TIMEOUT means that the sleep timed out.
210
 * @li  ESYNCH_TIMEOUT means that the sleep timed out.
210
 *
211
 *
211
 * @li ESYNCH_INTERRUPTED means that somebody interrupted the sleeping thread.
212
 * @li  ESYNCH_INTERRUPTED means that somebody interrupted the sleeping thread.
212
 *
213
 *
213
 * @li ESYNCH_OK_ATOMIC means that the sleep succeeded and that there was
214
 * @li  ESYNCH_OK_ATOMIC means that the sleep succeeded and that there was
214
 * a pending wakeup at the time of the call. The caller was not put
215
 *  a pending wakeup at the time of the call. The caller was not put
215
 * asleep at all.
216
 *  asleep at all.
216
 *
217
 *
217
 * @li ESYNCH_OK_BLOCKED means that the sleep succeeded; the full sleep was
218
 * @li  ESYNCH_OK_BLOCKED means that the sleep succeeded; the full sleep was
218
 * attempted.
219
 *  attempted.
219
 */
220
 */
220
int waitq_sleep_timeout(waitq_t *wq, uint32_t usec, int flags)
221
int waitq_sleep_timeout(waitq_t *wq, uint32_t usec, int flags)
221
{
222
{
222
    ipl_t ipl;
223
    ipl_t ipl;
223
    int rc;
224
    int rc;
Line 353... Line 354...
353
            spinlock_unlock(&THREAD->lock);
354
            spinlock_unlock(&THREAD->lock);
354
            return ESYNCH_TIMEOUT;
355
            return ESYNCH_TIMEOUT;
355
        }
356
        }
356
        THREAD->timeout_pending = true;
357
        THREAD->timeout_pending = true;
357
        timeout_register(&THREAD->sleep_timeout, (uint64_t) usec,
358
        timeout_register(&THREAD->sleep_timeout, (uint64_t) usec,
358
            waitq_timeouted_sleep, THREAD);
359
            waitq_sleep_timed_out, THREAD);
359
    }
360
    }
360
 
361
 
361
    list_append(&THREAD->wq_link, &wq->head);
362
    list_append(&THREAD->wq_link, &wq->head);
362
 
363
 
363
    /*
364
    /*
Line 429... Line 430...
429
   
430
   
430
    /*
431
    /*
431
     * Lock the thread prior to removing it from the wq.
432
     * Lock the thread prior to removing it from the wq.
432
     * This is not necessary because of mutual exclusion
433
     * This is not necessary because of mutual exclusion
433
     * (the link belongs to the wait queue), but because
434
     * (the link belongs to the wait queue), but because
434
     * of synchronization with waitq_timeouted_sleep()
435
     * of synchronization with waitq_sleep_timed_out()
435
     * and thread_interrupt_sleep().
436
     * and thread_interrupt_sleep().
436
     *
437
     *
437
     * In order for these two functions to work, the following
438
     * In order for these two functions to work, the following
438
     * invariant must hold:
439
     * invariant must hold:
439
     *
440
     *