[f3afd24] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Jakub Jermar
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libc
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[1e4cada] | 35 | #ifndef LIBC_FIBRIL_SYNCH_H_
|
---|
| 36 | #define LIBC_FIBRIL_SYNCH_H_
|
---|
[f3afd24] | 37 |
|
---|
| 38 | #include <async.h>
|
---|
| 39 | #include <fibril.h>
|
---|
| 40 | #include <adt/list.h>
|
---|
| 41 | #include <libarch/tls.h>
|
---|
[cadfa8e] | 42 | #include <sys/time.h>
|
---|
[f3afd24] | 43 |
|
---|
[668f8cbf] | 44 | typedef struct {
|
---|
| 45 | fibril_owner_info_t oi; /* Keep this the first thing. */
|
---|
[f3afd24] | 46 | int counter;
|
---|
| 47 | link_t waiters;
|
---|
| 48 | } fibril_mutex_t;
|
---|
| 49 |
|
---|
[e9460aa] | 50 | #define FIBRIL_MUTEX_INITIALIZER(name) \
|
---|
| 51 | { \
|
---|
[668f8cbf] | 52 | .oi = { \
|
---|
| 53 | .owned_by = NULL \
|
---|
| 54 | }, \
|
---|
[f3afd24] | 55 | .counter = 1, \
|
---|
| 56 | .waiters = { \
|
---|
| 57 | .prev = &name.waiters, \
|
---|
| 58 | .next = &name.waiters, \
|
---|
| 59 | } \
|
---|
| 60 | }
|
---|
[e9460aa] | 61 |
|
---|
| 62 | #define FIBRIL_MUTEX_INITIALIZE(name) \
|
---|
| 63 | fibril_mutex_t name = FIBRIL_MUTEX_INITIALIZER(name)
|
---|
[f3afd24] | 64 |
|
---|
| 65 | typedef struct {
|
---|
[668f8cbf] | 66 | fibril_owner_info_t oi; /* Keep this the first thing. */
|
---|
[92d34f0b] | 67 | unsigned writers;
|
---|
| 68 | unsigned readers;
|
---|
| 69 | link_t waiters;
|
---|
[f3afd24] | 70 | } fibril_rwlock_t;
|
---|
| 71 |
|
---|
[e9460aa] | 72 | #define FIBRIL_RWLOCK_INITIALIZER(name) \
|
---|
| 73 | { \
|
---|
[668f8cbf] | 74 | .oi = { \
|
---|
| 75 | .owned_by = NULL \
|
---|
| 76 | }, \
|
---|
[92d34f0b] | 77 | .readers = 0, \
|
---|
| 78 | .writers = 0, \
|
---|
| 79 | .waiters = { \
|
---|
| 80 | .prev = &name.waiters, \
|
---|
| 81 | .next = &name.waiters, \
|
---|
[f3afd24] | 82 | } \
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[e9460aa] | 85 | #define FIBRIL_RWLOCK_INITIALIZE(name) \
|
---|
| 86 | fibril_rwlock_t name = FIBRIL_RWLOCK_INITIALIZER(name)
|
---|
| 87 |
|
---|
[9ae22ba] | 88 | typedef struct {
|
---|
| 89 | link_t waiters;
|
---|
| 90 | } fibril_condvar_t;
|
---|
| 91 |
|
---|
[e9460aa] | 92 | #define FIBRIL_CONDVAR_INITIALIZER(name) \
|
---|
| 93 | { \
|
---|
[c51a7cd] | 94 | .waiters = { \
|
---|
| 95 | .next = &name.waiters, \
|
---|
| 96 | .prev = &name.waiters, \
|
---|
| 97 | } \
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[e9460aa] | 100 | #define FIBRIL_CONDVAR_INITIALIZE(name) \
|
---|
| 101 | fibril_condvar_t name = FIBRIL_CONDVAR_INITIALIZER(name)
|
---|
| 102 |
|
---|
[f3afd24] | 103 | extern void fibril_mutex_initialize(fibril_mutex_t *);
|
---|
| 104 | extern void fibril_mutex_lock(fibril_mutex_t *);
|
---|
| 105 | extern bool fibril_mutex_trylock(fibril_mutex_t *);
|
---|
| 106 | extern void fibril_mutex_unlock(fibril_mutex_t *);
|
---|
[b0a76d5] | 107 | extern bool fibril_mutex_is_locked(fibril_mutex_t *);
|
---|
[f3afd24] | 108 |
|
---|
| 109 | extern void fibril_rwlock_initialize(fibril_rwlock_t *);
|
---|
| 110 | extern void fibril_rwlock_read_lock(fibril_rwlock_t *);
|
---|
| 111 | extern void fibril_rwlock_write_lock(fibril_rwlock_t *);
|
---|
| 112 | extern void fibril_rwlock_read_unlock(fibril_rwlock_t *);
|
---|
| 113 | extern void fibril_rwlock_write_unlock(fibril_rwlock_t *);
|
---|
[b0a76d5] | 114 | extern bool fibril_rwlock_is_read_locked(fibril_rwlock_t *);
|
---|
| 115 | extern bool fibril_rwlock_is_write_locked(fibril_rwlock_t *);
|
---|
[f3afd24] | 116 |
|
---|
[9ae22ba] | 117 | extern void fibril_condvar_initialize(fibril_condvar_t *);
|
---|
[cadfa8e] | 118 | extern int fibril_condvar_wait_timeout(fibril_condvar_t *, fibril_mutex_t *,
|
---|
| 119 | suseconds_t);
|
---|
[9ae22ba] | 120 | extern void fibril_condvar_wait(fibril_condvar_t *, fibril_mutex_t *);
|
---|
| 121 | extern void fibril_condvar_signal(fibril_condvar_t *);
|
---|
| 122 | extern void fibril_condvar_broadcast(fibril_condvar_t *);
|
---|
| 123 |
|
---|
[f3afd24] | 124 | #endif
|
---|
| 125 |
|
---|
| 126 | /** @}
|
---|
| 127 | */
|
---|