[d73d992] | 1 | /*
|
---|
| 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
---|
| 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 | #ifndef LIBC_PRIVATE_FIBRIL_H_
|
---|
| 30 | #define LIBC_PRIVATE_FIBRIL_H_
|
---|
| 31 |
|
---|
| 32 | #include <adt/list.h>
|
---|
| 33 | #include <context.h>
|
---|
[0b05082] | 34 | #include <tls.h>
|
---|
[d73d992] | 35 | #include <abi/proc/uarg.h>
|
---|
[f787c8e] | 36 | #include <fibril.h>
|
---|
| 37 |
|
---|
| 38 | #include "./futex.h"
|
---|
| 39 |
|
---|
| 40 | typedef struct {
|
---|
| 41 | fibril_t *fibril;
|
---|
| 42 | } fibril_event_t;
|
---|
| 43 |
|
---|
| 44 | #define FIBRIL_EVENT_INIT ((fibril_event_t) {0})
|
---|
[d73d992] | 45 |
|
---|
| 46 | struct fibril {
|
---|
| 47 | // XXX: The first two fields must not move (for taskdump).
|
---|
| 48 | link_t all_link;
|
---|
| 49 | context_t ctx;
|
---|
| 50 |
|
---|
[514d561] | 51 | uspace_arg_t uarg;
|
---|
[d73d992] | 52 | link_t link;
|
---|
| 53 | void *stack;
|
---|
[514d561] | 54 | size_t stack_size;
|
---|
[d73d992] | 55 | void *arg;
|
---|
| 56 | errno_t (*func)(void *);
|
---|
| 57 | tcb_t *tcb;
|
---|
| 58 |
|
---|
| 59 | fibril_t *clean_after_me;
|
---|
| 60 | errno_t retval;
|
---|
| 61 |
|
---|
[514d561] | 62 | fibril_t *thread_ctx;
|
---|
[d73d992] | 63 |
|
---|
[514d561] | 64 | bool is_running : 1;
|
---|
[d73d992] | 65 | bool is_writer : 1;
|
---|
[40abf56] | 66 | /* In some places, we use fibril structs that can't be freed. */
|
---|
| 67 | bool is_freeable : 1;
|
---|
[d73d992] | 68 |
|
---|
[514d561] | 69 | /* Debugging stuff. */
|
---|
[2965d18] | 70 | int rmutex_locks;
|
---|
[514d561] | 71 | fibril_owner_info_t *waits_for;
|
---|
| 72 | fibril_event_t *sleep_event;
|
---|
| 73 | };
|
---|
[d73d992] | 74 |
|
---|
[40abf56] | 75 | extern fibril_t *fibril_alloc(void);
|
---|
| 76 | extern void fibril_setup(fibril_t *);
|
---|
[514d561] | 77 | extern void fibril_teardown(fibril_t *f);
|
---|
[d73d992] | 78 | extern fibril_t *fibril_self(void);
|
---|
| 79 |
|
---|
[514d561] | 80 | extern void __fibrils_init(void);
|
---|
[25f6bddb] | 81 | extern void __fibrils_fini(void);
|
---|
[514d561] | 82 |
|
---|
[f787c8e] | 83 | extern void fibril_wait_for(fibril_event_t *);
|
---|
[bd41ac52] | 84 | extern errno_t fibril_wait_timeout(fibril_event_t *, const struct timespec *);
|
---|
[f787c8e] | 85 | extern void fibril_notify(fibril_event_t *);
|
---|
| 86 |
|
---|
[bd41ac52] | 87 | extern errno_t fibril_ipc_wait(ipc_call_t *, const struct timespec *);
|
---|
[f787c8e] | 88 | extern void fibril_ipc_poke(void);
|
---|
| 89 |
|
---|
| 90 | /**
|
---|
| 91 | * "Restricted" fibril mutex.
|
---|
| 92 | *
|
---|
| 93 | * Similar to `fibril_mutex_t`, but has a set of restrictions placed on its
|
---|
| 94 | * use. Within a rmutex critical section, you
|
---|
| 95 | * - may not use any other synchronization primitive,
|
---|
| 96 | * save for another `fibril_rmutex_t`. This includes nonblocking
|
---|
| 97 | * operations like cvar signal and mutex unlock, unless otherwise
|
---|
| 98 | * specified.
|
---|
| 99 | * - may not read IPC messages
|
---|
| 100 | * - may not start a new thread/fibril
|
---|
| 101 | * (creating fibril without starting is fine)
|
---|
| 102 | *
|
---|
| 103 | * Additionally, locking with a timeout is not possible on this mutex,
|
---|
| 104 | * and there is no associated condition variable type.
|
---|
| 105 | * This is a design constraint, not a lack of implementation effort.
|
---|
| 106 | */
|
---|
| 107 | typedef struct {
|
---|
| 108 | // TODO: At this point, this is just silly handwaving to hide current
|
---|
| 109 | // futex use behind a fibril based abstraction. Later, the imple-
|
---|
| 110 | // mentation will change, but the restrictions placed on this type
|
---|
| 111 | // will allow it to be simpler and faster than a regular mutex.
|
---|
| 112 | // There might also be optional debug checking of the assumptions.
|
---|
| 113 | //
|
---|
| 114 | // Note that a consequence of the restrictions is that if we are
|
---|
| 115 | // running on a single thread, no other fibril can ever get to run
|
---|
| 116 | // while a fibril has a rmutex locked. That means that for
|
---|
| 117 | // single-threaded programs, we can reduce all rmutex locks and
|
---|
| 118 | // unlocks to simple branches on a global bool variable.
|
---|
| 119 |
|
---|
| 120 | futex_t futex;
|
---|
| 121 | } fibril_rmutex_t;
|
---|
| 122 |
|
---|
[45c8eea] | 123 | extern errno_t fibril_rmutex_initialize(fibril_rmutex_t *);
|
---|
[269bc459] | 124 | extern void fibril_rmutex_destroy(fibril_rmutex_t *);
|
---|
[f787c8e] | 125 | extern void fibril_rmutex_lock(fibril_rmutex_t *);
|
---|
| 126 | extern bool fibril_rmutex_trylock(fibril_rmutex_t *);
|
---|
| 127 | extern void fibril_rmutex_unlock(fibril_rmutex_t *);
|
---|
| 128 |
|
---|
[d73d992] | 129 | #endif
|
---|