| 1 |
/**************************************************************************/ |
| 2 |
/* */ |
| 3 |
/* This file is part of Frama-C. */ |
| 4 |
/* */ |
| 5 |
/* Copyright (C) 2007-2022 */ |
| 6 |
/* CEA (Commissariat à l'énergie atomique et aux énergies */ |
| 7 |
/* alternatives) */ |
| 8 |
/* */ |
| 9 |
/* you can redistribute it and/or modify it under the terms of the GNU */ |
| 10 |
/* Lesser General Public License as published by the Free Software */ |
| 11 |
/* Foundation, version 2.1. */ |
| 12 |
/* */ |
| 13 |
/* It is distributed in the hope that it will be useful, */ |
| 14 |
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ |
| 15 |
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ |
| 16 |
/* GNU Lesser General Public License for more details. */ |
| 17 |
/* */ |
| 18 |
/* See the GNU Lesser General Public License version 2.1 */ |
| 19 |
/* for more details (enclosed in the file licenses/LGPLv2.1). */ |
| 20 |
/* */ |
| 21 |
/**************************************************************************/ |
| 22 |
|
| 23 |
#ifndef __FC_PTHREAD_H__ |
| 24 |
#define __FC_PTHREAD_H__ |
| 25 |
|
| 26 |
#include "features.h" |
| 27 |
__PUSH_FC_STDLIB |
| 28 |
__BEGIN_DECLS |
| 29 |
|
| 30 |
#include "errno.h" |
| 31 |
#include "__fc_string_axiomatic.h" |
| 32 |
// The values for the constants below are based on an x86 Linux, |
| 33 |
// declared in the order given by POSIX.1-2008. |
| 34 |
|
| 35 |
enum __fc_pthread_cancelstate |
| 36 |
{ |
| 37 |
PTHREAD_CANCEL_ENABLE, |
| 38 |
PTHREAD_CANCEL_DISABLE |
| 39 |
}; |
| 40 |
|
| 41 |
enum __fc_pthread_canceltype |
| 42 |
{ |
| 43 |
PTHREAD_CANCEL_DEFERRED, |
| 44 |
PTHREAD_CANCEL_ASYNCHRONOUS |
| 45 |
}; |
| 46 |
|
| 47 |
enum __fc_pthread_detachstate |
| 48 |
{ |
| 49 |
PTHREAD_CREATE_JOINABLE, |
| 50 |
PTHREAD_CREATE_DETACHED |
| 51 |
}; |
| 52 |
|
| 53 |
enum __fc_pthread_inheritsched |
| 54 |
{ |
| 55 |
PTHREAD_INHERIT_SCHED, |
| 56 |
PTHREAD_EXPLICIT_SCHED |
| 57 |
}; |
| 58 |
|
| 59 |
|
| 60 |
/* Scope handling. */ |
| 61 |
enum __fc_pthread_scope |
| 62 |
{ |
| 63 |
PTHREAD_SCOPE_SYSTEM, |
| 64 |
PTHREAD_SCOPE_PROCESS |
| 65 |
}; |
| 66 |
|
| 67 |
enum __fc_pthread_mutex_type |
| 68 |
{ |
| 69 |
PTHREAD_MUTEX_NORMAL, |
| 70 |
PTHREAD_MUTEX_RECURSIVE, |
| 71 |
PTHREAD_MUTEX_ERRORCHECK, |
| 72 |
PTHREAD_MUTEX_DEFAULT |
| 73 |
}; |
| 74 |
|
| 75 |
enum __fc_pthread_mutex_robust |
| 76 |
{ |
| 77 |
PTHREAD_MUTEX_STALLED, |
| 78 |
PTHREAD_MUTEX_ROBUST |
| 79 |
}; |
| 80 |
|
| 81 |
enum __fc_pthread_mutex_protocol |
| 82 |
{ |
| 83 |
PTHREAD_PRIO_NONE, |
| 84 |
PTHREAD_PRIO_INHERIT, |
| 85 |
PTHREAD_PRIO_PROTECT |
| 86 |
}; |
| 87 |
|
| 88 |
enum __fc_pthread_mutex_pshared |
| 89 |
{ |
| 90 |
PTHREAD_PROCESS_PRIVATE, |
| 91 |
PTHREAD_PROCESS_SHARED |
| 92 |
}; |
| 93 |
|
| 94 |
#define PTHREAD_BARRIER_SERIAL_THREAD (-1) |
| 95 |
#define PTHREAD_CANCEL_ASYNCHRONOUS PTHREAD_CANCEL_ASYNCHRONOUS |
| 96 |
#define PTHREAD_CANCEL_ENABLE PTHREAD_CANCEL_ENABLE |
| 97 |
#define PTHREAD_CANCEL_DEFERRED PTHREAD_CANCEL_DEFERRED |
| 98 |
#define PTHREAD_CANCEL_DISABLE PTHREAD_CANCEL_DISABLE |
| 99 |
#define PTHREAD_CANCELED ((void *) -1) |
| 100 |
#define PTHREAD_CREATE_DETACHED PTHREAD_CREATE_DETACHED |
| 101 |
#define PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_JOINABLE |
| 102 |
|
| 103 |
#define PTHREAD_EXPLICIT_SCHED PTHREAD_EXPLICIT_SCHED |
| 104 |
#define PTHREAD_INHERIT_SCHED PTHREAD_INHERIT_SCHED |
| 105 |
|
| 106 |
#define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_DEFAULT |
| 107 |
#define PTHREAD_MUTEX_ERRORCHECK PTHREAD_MUTEX_ERRORCHECK |
| 108 |
#define PTHREAD_MUTEX_NORMAL PTHREAD_MUTEX_NORMAL |
| 109 |
#define PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_RECURSIVE |
| 110 |
#define PTHREAD_MUTEX_ROBUST PTHREAD_MUTEX_ROBUST |
| 111 |
#define PTHREAD_MUTEX_STALLED PTHREAD_MUTEX_STALLED |
| 112 |
#define PTHREAD_ONCE_INIT 0 |
| 113 |
|
| 114 |
#define PTHREAD_PRIO_INHERIT PTHREAD_PRIO_INHERIT |
| 115 |
|
| 116 |
#define PTHREAD_PRIO_NONE PTHREAD_PRIO_NONE |
| 117 |
|
| 118 |
#define PTHREAD_PRIO_PROTECT PTHREAD_PRIO_PROTECT |
| 119 |
|
| 120 |
#define PTHREAD_PROCESS_SHARED PTHREAD_PROCESS_SHARED |
| 121 |
#define PTHREAD_PROCESS_PRIVATE PTHREAD_PROCESS_PRIVATE |
| 122 |
|
| 123 |
#define PTHREAD_SCOPE_PROCESS PTHREAD_SCOPE_PROCESS |
| 124 |
#define PTHREAD_SCOPE_SYSTEM PTHREAD_SCOPE_SYSTEM |
| 125 |
|
| 126 |
#define PTHREAD_COND_INITIALIZER { 0 } |
| 127 |
#define PTHREAD_MUTEX_INITIALIZER { 0 } |
| 128 |
#define PTHREAD_RWLOCK_INITIALIZER { 0 } |
| 129 |
|
| 130 |
// Include pthread-related types |
| 131 |
#include "__fc_define_pthread_types.h" |
| 132 |
#include "__fc_define_size_t.h" |
| 133 |
#include "sched.h" |
| 134 |
|
| 135 |
extern int pthread_attr_destroy(pthread_attr_t *); |
| 136 |
extern int pthread_attr_getdetachstate(const pthread_attr_t *, int *); |
| 137 |
extern int pthread_attr_getguardsize(const pthread_attr_t *restrict, |
| 138 |
size_t *restrict); |
| 139 |
extern int pthread_attr_getinheritsched(const pthread_attr_t *restrict, |
| 140 |
int *restrict); |
| 141 |
extern int pthread_attr_getschedparam(const pthread_attr_t *restrict, |
| 142 |
struct sched_param *restrict); |
| 143 |
extern int pthread_attr_getschedpolicy(const pthread_attr_t *restrict, |
| 144 |
int *restrict); |
| 145 |
extern int pthread_attr_getscope(const pthread_attr_t *restrict, |
| 146 |
int *restrict); |
| 147 |
extern int pthread_attr_getstack(const pthread_attr_t *restrict, |
| 148 |
void **restrict, size_t *restrict); |
| 149 |
extern int pthread_attr_getstacksize(const pthread_attr_t *restrict, |
| 150 |
size_t *restrict); |
| 151 |
extern int pthread_attr_init(pthread_attr_t *); |
| 152 |
extern int pthread_attr_setdetachstate(pthread_attr_t *, int); |
| 153 |
extern int pthread_attr_setguardsize(pthread_attr_t *, size_t); |
| 154 |
extern int pthread_attr_setinheritsched(pthread_attr_t *, int); |
| 155 |
extern int pthread_attr_setschedparam(pthread_attr_t *restrict, |
| 156 |
const struct sched_param *restrict); |
| 157 |
extern int pthread_attr_setschedpolicy(pthread_attr_t *, int); |
| 158 |
extern int pthread_attr_setscope(pthread_attr_t *, int); |
| 159 |
extern int pthread_attr_setstack(pthread_attr_t *, void *, size_t); |
| 160 |
extern int pthread_attr_setstacksize(pthread_attr_t *, size_t); |
| 161 |
extern int pthread_barrier_destroy(pthread_barrier_t *); |
| 162 |
extern int pthread_barrier_init(pthread_barrier_t *restrict, |
| 163 |
const pthread_barrierattr_t *restrict, |
| 164 |
unsigned); |
| 165 |
extern int pthread_barrier_wait(pthread_barrier_t *); |
| 166 |
extern int pthread_barrierattr_destroy(pthread_barrierattr_t *); |
| 167 |
extern int pthread_barrierattr_getpshared(const pthread_barrierattr_t *restrict, |
| 168 |
int *restrict); |
| 169 |
extern int pthread_barrierattr_init(pthread_barrierattr_t *); |
| 170 |
extern int pthread_barrierattr_setpshared(pthread_barrierattr_t *, int); |
| 171 |
extern int pthread_cancel(pthread_t); |
| 172 |
|
| 173 |
/*@ requires valid_cond: \valid(cond); |
| 174 |
assigns \result \from \nothing; |
| 175 |
ensures sucess: \result == 0; |
| 176 |
*/ |
| 177 |
extern int pthread_cond_broadcast(pthread_cond_t *cond); |
| 178 |
|
| 179 |
/*@ |
| 180 |
requires valid_cond: \valid(cond); |
| 181 |
assigns \result \from indirect:*cond; |
| 182 |
ensures success_or_error: \result == 0 || \result == EBUSY; |
| 183 |
*/ |
| 184 |
extern int pthread_cond_destroy(pthread_cond_t * cond); |
| 185 |
|
| 186 |
/*@ |
| 187 |
requires valid_cond: \valid(cond); |
| 188 |
requires valid_null_attr: attr == \null || \valid_read(attr); |
| 189 |
assigns *cond \from *attr; |
| 190 |
assigns \result \from \nothing; |
| 191 |
ensures initialization:cond: \initialized(cond); |
| 192 |
ensures success: \result == 0; // at least on Linux |
| 193 |
// Note: "never returns an error" comes from the French manpage for pthreads |
| 194 |
// (http://manpagesfr.free.fr/man/man3/pthread_cond_init.3.html) |
| 195 |
*/ |
| 196 |
extern int pthread_cond_init(pthread_cond_t *restrict cond, |
| 197 |
const pthread_condattr_t *restrict attr); |
| 198 |
|
| 199 |
extern int pthread_cond_signal(pthread_cond_t *); |
| 200 |
extern int pthread_cond_timedwait(pthread_cond_t *restrict, |
| 201 |
pthread_mutex_t *restrict, |
| 202 |
const struct timespec *restrict); |
| 203 |
/*@ requires valid_cond: \valid(cond); |
| 204 |
requires valid_mutex: \valid(mutex); |
| 205 |
assigns \result \from \nothing; |
| 206 |
ensures success: \result == 0; |
| 207 |
// Note: "never returns an error" comes from the French manpage for pthreads |
| 208 |
// (http://manpagesfr.free.fr/man/man3/pthread_cond_init.3.html) |
| 209 |
*/ |
| 210 |
extern int pthread_cond_wait(pthread_cond_t *restrict cond, |
| 211 |
pthread_mutex_t *restrict mutex); |
| 212 |
extern int pthread_condattr_destroy(pthread_condattr_t *); |
| 213 |
//clockid_t not available yet |
| 214 |
//extern int pthread_condattr_getclock(const pthread_condattr_t *restrict, |
| 215 |
// clockid_t *restrict); |
| 216 |
extern int pthread_condattr_getpshared(const pthread_condattr_t *restrict, |
| 217 |
int *restrict); |
| 218 |
extern int pthread_condattr_init(pthread_condattr_t *); |
| 219 |
//clockid_t not available yet |
| 220 |
//extern int pthread_condattr_setclock(pthread_condattr_t *, clockid_t); |
| 221 |
extern int pthread_condattr_setpshared(pthread_condattr_t *, int); |
| 222 |
|
| 223 |
/*@ requires valid_thread: \valid(thread); |
| 224 |
requires valid_null_attr: attr == \null || \valid_read(attr); |
| 225 |
requires valid_routine: \valid_function(start_routine); |
| 226 |
requires valid_null_arg: arg == \null || \valid((char*)arg); |
| 227 |
assigns *thread \from *attr; |
| 228 |
assigns \result \from indirect:*attr; |
| 229 |
ensures success_or_error: |
| 230 |
\result == 0 || |
| 231 |
\result == EAGAIN || \result == EINVAL || \result == EPERM; |
| 232 |
*/ |
| 233 |
extern int pthread_create(pthread_t *restrict thread, |
| 234 |
const pthread_attr_t *restrict attr, |
| 235 |
void *(*start_routine)(void*), |
| 236 |
void *restrict arg); |
| 237 |
extern int pthread_detach(pthread_t); |
| 238 |
extern int pthread_equal(pthread_t, pthread_t); |
| 239 |
extern void pthread_exit(void *); |
| 240 |
extern int pthread_getconcurrency(void); |
| 241 |
//clockid_t not available yet |
| 242 |
//extern int pthread_getcpuclockid(pthread_t, clockid_t *); |
| 243 |
extern int pthread_getschedparam(pthread_t, int *restrict, |
| 244 |
struct sched_param *restrict); |
| 245 |
extern void *pthread_getspecific(pthread_key_t); |
| 246 |
|
| 247 |
/*@ requires valid_or_null_retval: retval == \null || \valid(retval); |
| 248 |
assigns *retval \from thread; |
| 249 |
assigns \result \from indirect:thread; |
| 250 |
ensures success_or_error: |
| 251 |
\result == 0 || |
| 252 |
\result == EDEADLK || \result == EINVAL || \result == ESRCH; |
| 253 |
|
| 254 |
behavior ignore_retval: |
| 255 |
assumes null_retval: retval == \null; |
| 256 |
assigns \result \from indirect:thread; |
| 257 |
|
| 258 |
behavior use_retval: |
| 259 |
assumes valid_retval: \valid(retval); |
| 260 |
assigns *retval \from thread; |
| 261 |
assigns \result \from indirect:thread; |
| 262 |
*/ |
| 263 |
extern int pthread_join(pthread_t thread, void **retval); |
| 264 |
|
| 265 |
extern int pthread_key_create(pthread_key_t *, void (*)(void*)); |
| 266 |
extern int pthread_key_delete(pthread_key_t); |
| 267 |
extern int pthread_mutex_consistent(pthread_mutex_t *); |
| 268 |
|
| 269 |
/*@ requires mutex_valid: \valid(mutex); |
| 270 |
assigns *mutex \from *mutex; |
| 271 |
assigns \result \from indirect:*mutex; |
| 272 |
ensures init_or_busy: \result == 0 || \result == EBUSY; |
| 273 |
*/ |
| 274 |
extern int pthread_mutex_destroy(pthread_mutex_t *mutex); |
| 275 |
|
| 276 |
extern int pthread_mutex_getprioceiling(const pthread_mutex_t *restrict, |
| 277 |
int *restrict); |
| 278 |
/*@ |
| 279 |
requires mutex_valid: \valid(mutex); |
| 280 |
requires attrs_valid_or_null: attrs == \null || \valid_read(attrs); |
| 281 |
assigns *mutex \from *mutex, *attrs; |
| 282 |
assigns \result \from indirect:*mutex, indirect:*attrs; |
| 283 |
// NB: under Linux, \result is guaranteed to be 0. |
| 284 |
ensures initialization:success_or_error: |
| 285 |
(\result == 0 && \initialized(mutex)) |
| 286 |
|| \result == EAGAIN |
| 287 |
|| \result == ENOMEM |
| 288 |
|| \result == EPERM |
| 289 |
|| \result == EINVAL; |
| 290 |
*/ |
| 291 |
extern int pthread_mutex_init(pthread_mutex_t *restrict mutex, |
| 292 |
const pthread_mutexattr_t *restrict attrs); |
| 293 |
|
| 294 |
/*@ |
| 295 |
requires mutex_valid: \valid(mutex); |
| 296 |
assigns *mutex \from *mutex; |
| 297 |
assigns \result \from indirect:*mutex; |
| 298 |
ensures success_or_error: |
| 299 |
\result == 0 |
| 300 |
|| \result == EAGAIN |
| 301 |
|| \result == EINVAL |
| 302 |
|| \result == EDEADLK; |
| 303 |
// NB: more error codes are specified in POSIX, but they are not |
| 304 |
// exported by our version of errno.h |
| 305 |
*/ |
| 306 |
extern int pthread_mutex_lock(pthread_mutex_t * mutex); |
| 307 |
extern int pthread_mutex_setprioceiling(pthread_mutex_t *restrict, int, |
| 308 |
int *restrict); |
| 309 |
extern int pthread_mutex_timedlock(pthread_mutex_t *restrict, |
| 310 |
const struct timespec *restrict); |
| 311 |
extern int pthread_mutex_trylock(pthread_mutex_t *); |
| 312 |
/*@ |
| 313 |
requires mutex_valid: \valid(mutex); |
| 314 |
assigns *mutex \from *mutex; |
| 315 |
assigns \result \from indirect:*mutex; |
| 316 |
ensures success_or_error: \result == 0 || \result == EPERM; |
| 317 |
*/ |
| 318 |
extern int pthread_mutex_unlock(pthread_mutex_t *mutex); |
| 319 |
|
| 320 |
/*@ |
| 321 |
requires valid_attr: \valid(attr); |
| 322 |
assigns *attr \from *attr; |
| 323 |
assigns \result \from indirect:*attr; |
| 324 |
ensures success_or_error: \result == 0 || \result == EINVAL; |
| 325 |
*/ |
| 326 |
extern int pthread_mutexattr_destroy(pthread_mutexattr_t *attr); |
| 327 |
|
| 328 |
extern int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *restrict, |
| 329 |
int *restrict); |
| 330 |
extern int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *restrict, |
| 331 |
int *restrict); |
| 332 |
extern int pthread_mutexattr_getpshared(const pthread_mutexattr_t *restrict, |
| 333 |
int *restrict); |
| 334 |
extern int pthread_mutexattr_getrobust(const pthread_mutexattr_t *restrict, |
| 335 |
int *restrict); |
| 336 |
extern int pthread_mutexattr_gettype(const pthread_mutexattr_t *restrict, |
| 337 |
int *restrict); |
| 338 |
|
| 339 |
/*@ |
| 340 |
requires valid_attr: \valid(attr); |
| 341 |
assigns \result, *attr \from \nothing; |
| 342 |
ensures success_or_error: \result == 0 || \result == ENOMEM; |
| 343 |
*/ |
| 344 |
extern int pthread_mutexattr_init(pthread_mutexattr_t *attr); |
| 345 |
|
| 346 |
extern int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *, int); |
| 347 |
extern int pthread_mutexattr_setprotocol(pthread_mutexattr_t *, int); |
| 348 |
extern int pthread_mutexattr_setpshared(pthread_mutexattr_t *, int); |
| 349 |
extern int pthread_mutexattr_setrobust(pthread_mutexattr_t *, int); |
| 350 |
|
| 351 |
/*@ |
| 352 |
requires valid_attr: \valid(attr); |
| 353 |
assigns \result, *attr \from indirect:type; |
| 354 |
ensures success_or_error: \result == 0 || \result == EINVAL; |
| 355 |
*/ |
| 356 |
extern int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type); |
| 357 |
|
| 358 |
extern int pthread_once(pthread_once_t *, void (*)(void)); |
| 359 |
extern int pthread_rwlock_destroy(pthread_rwlock_t *); |
| 360 |
extern int pthread_rwlock_init(pthread_rwlock_t *restrict, |
| 361 |
const pthread_rwlockattr_t *restrict); |
| 362 |
extern int pthread_rwlock_rdlock(pthread_rwlock_t *); |
| 363 |
extern int pthread_rwlock_timedrdlock(pthread_rwlock_t *restrict, |
| 364 |
const struct timespec *restrict); |
| 365 |
extern int pthread_rwlock_timedwrlock(pthread_rwlock_t *restrict, |
| 366 |
const struct timespec *restrict); |
| 367 |
extern int pthread_rwlock_tryrdlock(pthread_rwlock_t *); |
| 368 |
extern int pthread_rwlock_trywrlock(pthread_rwlock_t *); |
| 369 |
extern int pthread_rwlock_unlock(pthread_rwlock_t *); |
| 370 |
extern int pthread_rwlock_wrlock(pthread_rwlock_t *); |
| 371 |
extern int pthread_rwlockattr_destroy(pthread_rwlockattr_t *); |
| 372 |
extern int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *restrict, |
| 373 |
int *restrict); |
| 374 |
extern int pthread_rwlockattr_init(pthread_rwlockattr_t *); |
| 375 |
extern int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *, int); |
| 376 |
extern pthread_t pthread_self(void); |
| 377 |
extern int pthread_setcancelstate(int, int *); |
| 378 |
extern int pthread_setcanceltype(int, int *); |
| 379 |
extern int pthread_setconcurrency(int); |
| 380 |
extern int pthread_setschedparam(pthread_t, int, const struct sched_param *); |
| 381 |
extern int pthread_setschedprio(pthread_t, int); |
| 382 |
extern int pthread_setspecific(pthread_key_t, const void *); |
| 383 |
extern int pthread_spin_destroy(pthread_spinlock_t *); |
| 384 |
extern int pthread_spin_init(pthread_spinlock_t *, int); |
| 385 |
extern int pthread_spin_lock(pthread_spinlock_t *); |
| 386 |
extern int pthread_spin_trylock(pthread_spinlock_t *); |
| 387 |
extern int pthread_spin_unlock(pthread_spinlock_t *); |
| 388 |
extern void pthread_testcancel(void); |
| 389 |
|
| 390 |
// GNU extensions |
| 391 |
|
| 392 |
/*@ requires valid_name: valid_read_string(name); |
| 393 |
assigns \result \from indirect:thread, indirect:name[0 .. strlen(name)]; |
| 394 |
*/ |
| 395 |
int pthread_setname_np(pthread_t thread, const char* name); |
| 396 |
|
| 397 |
/*@ requires valid_name: \valid(name + (0 .. len - 1)); |
| 398 |
assigns \result, name[0 .. len - 1] \from thread; |
| 399 |
ensures name_written: initialization: |
| 400 |
\result == 0 ==> |
| 401 |
valid_string(name) && |
| 402 |
\initialized(name + (0 .. strlen(name))); |
| 403 |
*/ |
| 404 |
int pthread_getname_np(pthread_t thread, char* name, size_t len); |
| 405 |
|
| 406 |
// From POSIX: "Inclusion of the <pthread.h> header shall make symbols defined |
| 407 |
// in the headers <sched.h> and <time.h> visible." |
| 408 |
// (sched.h has already been included) |
| 409 |
#include "time.h" |
| 410 |
|
| 411 |
__END_DECLS |
| 412 |
__POP_FC_STDLIB |
| 413 |
#endif |