1 /* Kernel specific structures and functions 2 * 3 * Copyright 2004-2006, Haiku Inc. All Rights Reserved. 4 * Distributed under the terms of the MIT License. 5 */ 6 #ifndef _FSSH_SEM_H 7 #define _FSSH_SEM_H 8 9 #include "fssh_types.h" 10 11 12 #ifdef __cplusplus 13 extern "C" { 14 #endif 15 16 17 /*-------------------------------------------------------------*/ 18 /* System constants */ 19 20 #define FSSH_B_OS_NAME_LENGTH 32 21 #define FSSH_B_PAGE_SIZE 4096 22 #define FSSH_B_INFINITE_TIMEOUT (9223372036854775807LL) 23 24 25 /*-------------------------------------------------------------*/ 26 /* Types */ 27 28 typedef int32_t fssh_area_id; 29 typedef int32_t fssh_port_id; 30 typedef int32_t fssh_sem_id; 31 typedef int32_t fssh_team_id; 32 typedef int32_t fssh_thread_id; 33 34 35 /*-------------------------------------------------------------*/ 36 /* Semaphores */ 37 38 typedef struct fssh_sem_info { 39 fssh_sem_id sem; 40 fssh_team_id team; 41 char name[FSSH_B_OS_NAME_LENGTH]; 42 int32_t count; 43 fssh_thread_id latest_holder; 44 } fssh_sem_info; 45 46 /* semaphore flags */ 47 enum { 48 FSSH_B_CAN_INTERRUPT = 0x01, // acquisition of the semaphore can be 49 // interrupted (system use only) 50 FSSH_B_CHECK_PERMISSION = 0x04, // ownership will be checked (system use 51 // only) 52 FSSH_B_KILL_CAN_INTERRUPT = 0x20, // acquisition of the semaphore can be 53 // interrupted by SIGKILL[THR], even 54 // if not B_CAN_INTERRUPT (system use 55 // only) 56 57 /* release_sem_etc() only flags */ 58 FSSH_B_DO_NOT_RESCHEDULE = 0x02, // thread is not rescheduled 59 FSSH_B_RELEASE_ALL = 0x08, // all waiting threads will be woken up, 60 // count will be zeroed 61 FSSH_B_RELEASE_IF_WAITING_ONLY = 0x10 // release count only if there are any 62 // threads waiting 63 }; 64 65 extern fssh_sem_id fssh_create_sem(int32_t count, const char *name); 66 extern fssh_status_t fssh_delete_sem(fssh_sem_id id); 67 extern fssh_status_t fssh_acquire_sem(fssh_sem_id id); 68 extern fssh_status_t fssh_acquire_sem_etc(fssh_sem_id id, int32_t count, 69 uint32_t flags, fssh_bigtime_t timeout); 70 extern fssh_status_t fssh_release_sem(fssh_sem_id id); 71 extern fssh_status_t fssh_release_sem_etc(fssh_sem_id id, int32_t count, 72 uint32_t flags); 73 extern fssh_status_t fssh_get_sem_count(fssh_sem_id id, 74 int32_t *threadCount); 75 extern fssh_status_t fssh_set_sem_owner(fssh_sem_id id, fssh_team_id team); 76 77 /* system private, use the macros instead */ 78 extern fssh_status_t _fssh_get_sem_info(fssh_sem_id id, 79 struct fssh_sem_info *info, fssh_size_t infoSize); 80 extern fssh_status_t _fssh_get_next_sem_info(fssh_team_id team, 81 int32_t *cookie, struct fssh_sem_info *info, 82 fssh_size_t infoSize); 83 84 #define fssh_get_sem_info(sem, info) \ 85 _fssh_get_sem_info((sem), (info), sizeof(*(info))) 86 87 #define fssh_get_next_sem_info(team, cookie, info) \ 88 _fssh_get_next_sem_info((team), (cookie), (info), sizeof(*(info))) 89 90 91 92 enum { 93 FSSH_B_TIMEOUT = 8, /* relative timeout */ 94 FSSH_B_RELATIVE_TIMEOUT = 8, /* fails after a relative timeout with B_WOULD_BLOCK */ 95 FSSH_B_ABSOLUTE_TIMEOUT = 16 /* fails after an absolute timeout with B_WOULD BLOCK */ 96 }; 97 98 99 /*-------------------------------------------------------------*/ 100 /* Teams */ 101 102 #define FSSH_B_CURRENT_TEAM 0 103 #define FSSH_B_SYSTEM_TEAM 1 104 105 106 /*-------------------------------------------------------------*/ 107 /* Threads */ 108 109 typedef enum { 110 FSSH_B_THREAD_RUNNING = 1, 111 FSSH_B_THREAD_READY, 112 FSSH_B_THREAD_RECEIVING, 113 FSSH_B_THREAD_ASLEEP, 114 FSSH_B_THREAD_SUSPENDED, 115 FSSH_B_THREAD_WAITING 116 } fssh_thread_state; 117 118 typedef struct { 119 fssh_thread_id thread; 120 fssh_team_id team; 121 char name[FSSH_B_OS_NAME_LENGTH]; 122 fssh_thread_state state; 123 int32_t priority; 124 fssh_sem_id sem; 125 fssh_bigtime_t user_time; 126 fssh_bigtime_t kernel_time; 127 void *stack_base; 128 void *stack_end; 129 } fssh_thread_info; 130 131 #define FSSH_B_IDLE_PRIORITY 0 132 #define FSSH_B_LOWEST_ACTIVE_PRIORITY 1 133 #define FSSH_B_LOW_PRIORITY 5 134 #define FSSH_B_NORMAL_PRIORITY 10 135 #define FSSH_B_DISPLAY_PRIORITY 15 136 #define FSSH_B_URGENT_DISPLAY_PRIORITY 20 137 #define FSSH_B_REAL_TIME_DISPLAY_PRIORITY 100 138 #define FSSH_B_URGENT_PRIORITY 110 139 #define FSSH_B_REAL_TIME_PRIORITY 120 140 141 #define FSSH_B_FIRST_REAL_TIME_PRIORITY B_REAL_TIME_DISPLAY_PRIORITY 142 #define FSSH_B_MIN_PRIORITY B_IDLE_PRIORITY 143 #define FSSH_B_MAX_PRIORITY B_REAL_TIME_PRIORITY 144 145 #define FSSH_B_SYSTEM_TIMEBASE 0 146 147 typedef fssh_status_t (*fssh_thread_func)(void *); 148 #define fssh_thread_entry fssh_thread_func 149 /* thread_entry is for backward compatibility only! Use thread_func */ 150 151 extern fssh_thread_id fssh_spawn_thread(fssh_thread_func, const char *name, 152 int32_t priority, void *data); 153 extern fssh_status_t fssh_kill_thread(fssh_thread_id thread); 154 extern fssh_status_t fssh_resume_thread(fssh_thread_id thread); 155 extern fssh_status_t fssh_suspend_thread(fssh_thread_id thread); 156 157 extern fssh_status_t fssh_rename_thread(fssh_thread_id thread, 158 const char *newName); 159 extern fssh_status_t fssh_set_thread_priority (fssh_thread_id thread, 160 int32_t newPriority); 161 extern void fssh_exit_thread(fssh_status_t status); 162 extern fssh_status_t fssh_wait_for_thread (fssh_thread_id thread, 163 fssh_status_t *threadReturnValue); 164 extern fssh_status_t fssh_on_exit_thread(void (*callback)(void *), 165 void *data); 166 167 extern fssh_thread_id fssh_find_thread(const char *name); 168 169 extern fssh_status_t fssh_send_data(fssh_thread_id thread, int32_t code, 170 const void *buffer, 171 fssh_size_t bufferSize); 172 extern int32_t fssh_receive_data(fssh_thread_id *sender, void *buffer, 173 fssh_size_t bufferSize); 174 extern bool fssh_has_data(fssh_thread_id thread); 175 176 extern fssh_status_t fssh_snooze(fssh_bigtime_t amount); 177 extern fssh_status_t fssh_snooze_etc(fssh_bigtime_t amount, int timeBase, 178 uint32_t flags); 179 extern fssh_status_t fssh_snooze_until(fssh_bigtime_t time, int timeBase); 180 181 /* system private, use macros instead */ 182 extern fssh_status_t _fssh_get_thread_info(fssh_thread_id id, 183 fssh_thread_info *info, fssh_size_t size); 184 extern fssh_status_t _fssh_get_next_thread_info(fssh_team_id team, 185 int32_t *cookie, fssh_thread_info *info, 186 fssh_size_t size); 187 188 #define fssh_get_thread_info(id, info) \ 189 _fssh_get_thread_info((id), (info), sizeof(*(info))) 190 191 #define fssh_get_next_thread_info(team, cookie, info) \ 192 _fssh_get_next_thread_info((team), (cookie), (info), sizeof(*(info))) 193 194 195 /*-------------------------------------------------------------*/ 196 /* Time */ 197 198 extern uint32_t fssh_real_time_clock(void); 199 extern void fssh_set_real_time_clock(uint32_t secs_since_jan1_1970); 200 extern fssh_bigtime_t fssh_real_time_clock_usecs(void); 201 extern fssh_status_t fssh_set_timezone(char *timezone); 202 extern fssh_bigtime_t fssh_system_time(void); /* time since booting in microseconds */ 203 204 205 #ifdef __cplusplus 206 } 207 #endif 208 209 210 #endif // _FSSH_TYPES_H 211