Subversion Repositories HelenOS

Rev

Rev 2787 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2787 Rev 2925
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 69... Line 69...
69
    wq->missed_wakeups = 0;
69
    wq->missed_wakeups = 0;
70
}
70
}
71
 
71
 
72
/** Handle timeout during waitq_sleep_timeout() call
72
/** Handle timeout during waitq_sleep_timeout() call
73
 *
73
 *
74
 * This routine is called when waitq_sleep_timeout() timeouts.
74
 * This routine is called when waitq_sleep_timeout() times out.
75
 * Interrupts are disabled.
75
 * Interrupts are disabled.
76
 *
76
 *
77
 * It is supposed to try to remove 'its' thread from the wait queue;
77
 * It is supposed to try to remove 'its' thread from the wait queue;
78
 * it can eventually fail to achieve this goal when these two events
78
 * it can eventually fail to achieve this goal when these two events
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 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
 *
Line 289... Line 290...
289
}
290
}
290
 
291
 
291
/** Internal implementation of waitq_sleep_timeout().
292
/** Internal implementation of waitq_sleep_timeout().
292
 *
293
 *
293
 * This function implements logic of sleeping in a wait queue.
294
 * This function implements logic of sleeping in a wait queue.
294
 * This call must be preceeded by a call to waitq_sleep_prepare()
295
 * This call must be preceded by a call to waitq_sleep_prepare()
295
 * and followed by a call to waitq_slee_finish().
296
 * and followed by a call to waitq_sleep_finish().
296
 *
297
 *
297
 * @param wq See waitq_sleep_timeout().
298
 * @param wq        See waitq_sleep_timeout().
298
 * @param usec See waitq_sleep_timeout().
299
 * @param usec      See waitq_sleep_timeout().
299
 * @param flags See waitq_sleep_timeout().
300
 * @param flags     See waitq_sleep_timeout().
300
 *
301
 *
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 403... Line 404...
403
 *
404
 *
404
 * This is the internal SMP- and IRQ-unsafe version of waitq_wakeup(). It
405
 * This is the internal SMP- and IRQ-unsafe version of waitq_wakeup(). It
405
 * assumes wq->lock is already locked and interrupts are already disabled.
406
 * assumes wq->lock is already locked and interrupts are already disabled.
406
 *
407
 *
407
 * @param wq    Pointer to wait queue.
408
 * @param wq        Pointer to wait queue.
408
 * @param mode  If mode is WAKEUP_FIRST, then the longest waiting thread,
409
 * @param mode      If mode is WAKEUP_FIRST, then the longest waiting
409
 *      if any, is woken up. If mode is WAKEUP_ALL, then all
410
 *          thread, if any, is woken up. If mode is WAKEUP_ALL, then
410
 *      waiting threads, if any, are woken up. If there are no
411
 *          all waiting threads, if any, are woken up. If there are
411
 *      waiting threads to be woken up, the missed wakeup is
412
 *          no waiting threads to be woken up, the missed wakeup is
412
 *      recorded in the wait queue.
413
 *          recorded in the wait queue.
413
 */
414
 */
414
void _waitq_wakeup_unsafe(waitq_t *wq, wakeup_mode_t mode)
415
void _waitq_wakeup_unsafe(waitq_t *wq, wakeup_mode_t mode)
415
{
416
{
416
    thread_t *t;
417
    thread_t *t;
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
     *