| 1 | extern void *value = NULL;
|
|---|
| 2 | __pthread_pool_t _pool={.threads=NULL, .len = 0};
|
|---|
| 3 |
|
|---|
| 4 | void _add_thread(__pthread_pool_t* pool, pthread_t* thread){
|
|---|
| 5 | int len = pool->len;
|
|---|
| 6 | pthread_t** newThreads = (pthread_t**) malloc(sizeof(pthread_t*) * (len+1));
|
|---|
| 7 |
|
|---|
| 8 | if(pool->threads != NULL)
|
|---|
| 9 | for(int i = 0; i < len - 1; i++)
|
|---|
| 10 | *(newThreads+i) = *(pool->threads+i);
|
|---|
| 11 | *(newThreads+len) = thread;
|
|---|
| 12 | pool->threads = newThreads;
|
|---|
| 13 | pool->len = len + 1;
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * Initializes an attribute with the default values defined for it by an implementation.
|
|---|
| 18 | * Corresponding specification: p. 1532-4
|
|---|
| 19 | *
|
|---|
| 20 | * @param *attr
|
|---|
| 21 | * The attribute to be initialized.
|
|---|
| 22 | * @return Returns 0 upon successful completion
|
|---|
| 23 | */
|
|---|
| 24 |
|
|---|
| 25 | int pthread_attr_init(pthread_attr_t *attr){
|
|---|
| 26 | attr->stacksize = 0;
|
|---|
| 27 | attr->guardsize = 0;
|
|---|
| 28 | attr->detachstate = PTHREAD_CREATE_DETACHED;
|
|---|
| 29 | //attr->inheritsched = PTHREAD_EXPLICIT_SCHED;
|
|---|
| 30 | attr->contentionscope = PTHREAD_SCOPE_SYSTEM;
|
|---|
| 31 | attr->stackaddr = NULL;
|
|---|
| 32 | //attr->schedpolicy = SCHED_OTHER;
|
|---|
| 33 | return 0;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * Uninitializes the specified attr variable.
|
|---|
| 38 | * Corresponding specification: p. 1532-4
|
|---|
| 39 | *
|
|---|
| 40 | * @param *attr
|
|---|
| 41 | * The attribute to be uninitialized.
|
|---|
| 42 | * @return Returns 0 upon successful completion
|
|---|
| 43 | */
|
|---|
| 44 |
|
|---|
| 45 | int pthread_attr_destroy(pthread_attr_t *attr)
|
|---|
| 46 | {
|
|---|
| 47 | pthread_attr_t blank;
|
|---|
| 48 |
|
|---|
| 49 | *attr = blank;
|
|---|
| 50 | return 0;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | /**
|
|---|
| 55 | * Sets the detachstate field of the attribute
|
|---|
| 56 | * Corresponding specification: p. 1535-6
|
|---|
| 57 | *
|
|---|
| 58 | * @param *attr
|
|---|
| 59 | * The attribute to have it's detachstate set
|
|---|
| 60 | * @param detachstate
|
|---|
| 61 | * The detachstate to which the attribute's detachstate is set
|
|---|
| 62 | * @return Returns 0 upon successful completion
|
|---|
| 63 | */
|
|---|
| 64 |
|
|---|
| 65 | int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
|
|---|
| 66 | {
|
|---|
| 67 | attr->detachstate = detachstate;
|
|---|
| 68 | return 0;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | /**
|
|---|
| 72 | * Stores the detachstate value of the attribute in an alternate location
|
|---|
| 73 | * Corresponding specification: p. 1535-6
|
|---|
| 74 | *
|
|---|
| 75 | * @param *attr
|
|---|
| 76 | * The attribute whose detachstate is to be stored
|
|---|
| 77 | * @param *detachstate
|
|---|
| 78 | * The location at which the detachstate is to be stored
|
|---|
| 79 | * @return Returns 0 upon successful completion
|
|---|
| 80 | */
|
|---|
| 81 |
|
|---|
| 82 | int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
|
|---|
| 83 | {
|
|---|
| 84 | *detachstate = attr->detachstate;
|
|---|
| 85 | return 0;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * Set scheduling inheritance
|
|---|
| 90 | * Corresponding specification: p. 1540-1
|
|---|
| 91 | *
|
|---|
| 92 | * @param *attr
|
|---|
| 93 | * The attribute to have it's inheritsched set
|
|---|
| 94 | * @param inheritsched
|
|---|
| 95 | * The inheritsched to which the attribute's inheritsched is set
|
|---|
| 96 | * @return Returns 0 upon successful completion
|
|---|
| 97 | */
|
|---|
| 98 |
|
|---|
| 99 | int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched)
|
|---|
| 100 | {
|
|---|
| 101 | attr->inheritsched = inheritsched;
|
|---|
| 102 | return 0;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /**
|
|---|
| 106 | * Stores the inheritsched value of the attribute in an alternate location
|
|---|
| 107 | * Corresponding specification: p. 1540-1
|
|---|
| 108 | *
|
|---|
| 109 | * @param *attr
|
|---|
| 110 | * The attribute whose inheritsched is to be stored
|
|---|
| 111 | * @param *intheritsched
|
|---|
| 112 | * The location at which the attribute's inheritsched is to be stored
|
|---|
| 113 | * @return Returns 0 upon successful completion
|
|---|
| 114 | */
|
|---|
| 115 |
|
|---|
| 116 | int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inheritsched)
|
|---|
| 117 | {
|
|---|
| 118 | *inheritsched = attr->inheritsched;
|
|---|
| 119 | return 0;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /**
|
|---|
| 123 | * Set contentionscope field of the attribute
|
|---|
| 124 | * Corresponding specification: p. 1546-7
|
|---|
| 125 | *
|
|---|
| 126 | * @param *attr
|
|---|
| 127 | * The attribute to have it's contentionscope set
|
|---|
| 128 | * @param contentionscope
|
|---|
| 129 | * The contentionscope to which the attribute's contentionscope is set
|
|---|
| 130 | * @return Returns 0 upon successful completion
|
|---|
| 131 | */
|
|---|
| 132 |
|
|---|
| 133 | int pthread_attr_setscope(pthread_attr_t *attr, int contentionscope)
|
|---|
| 134 | {
|
|---|
| 135 | attr->contentionscope = contentionscope;
|
|---|
| 136 | return 0;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /**
|
|---|
| 140 | * Stores the contentionscope value of the attribute in an alternate location
|
|---|
| 141 | * Corresponding specification: p. 1546-7
|
|---|
| 142 | *
|
|---|
| 143 | * @param *attr
|
|---|
| 144 | * The attribute whose contentionscope is to be stored
|
|---|
| 145 | * @param *contentionscope
|
|---|
| 146 | * The location at which the attribute's contentionscope is to be stored
|
|---|
| 147 | * @return Returns 0 upon successful completion
|
|---|
| 148 | */
|
|---|
| 149 |
|
|---|
| 150 | int pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope)
|
|---|
| 151 | {
|
|---|
| 152 | *contentionscope = attr->contentionscope;
|
|---|
| 153 | return 0;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 | /**
|
|---|
| 158 | * Sets the stack address and stacksize fields of the attribute
|
|---|
| 159 | * Corresponding specification: p. 1548-50
|
|---|
| 160 | *
|
|---|
| 161 | * @param *attr
|
|---|
| 162 | * The attribute to have it's stack address and stack size set
|
|---|
| 163 | * @param *stackaddr
|
|---|
| 164 | * The address to which the attribute's stack address is set
|
|---|
| 165 | * @param stacksize
|
|---|
| 166 | * The size to which the attribute's stack size is set
|
|---|
| 167 | * @return Returns 0 upon successful completion
|
|---|
| 168 | */
|
|---|
| 169 |
|
|---|
| 170 | int pthread_attr_setstack(pthread_attr_t *attr, void * stackaddr, size_t stacksize){
|
|---|
| 171 | attr->stackaddr = stackaddr;
|
|---|
| 172 | attr->stacksize = stacksize;
|
|---|
| 173 | return 0;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | /**
|
|---|
| 177 | * Stores the stack address and size values in alternate locations
|
|---|
| 178 | * Corresponding specification: p. 1548-50
|
|---|
| 179 | *
|
|---|
| 180 | * @param *attr
|
|---|
| 181 | * The attribute whose stack address and size are to be stored
|
|---|
| 182 | * @param **stackaddr
|
|---|
| 183 | * The location at which the attribute's stackaddr is to be stored
|
|---|
| 184 | * @param *stacksize
|
|---|
| 185 | * The location at which the attribute's stacksize is to be stored
|
|---|
| 186 | * @return Returns 0 upon successful completion
|
|---|
| 187 | */
|
|---|
| 188 | // Get the stack size and stack address of the specified pthread_attr_t
|
|---|
| 189 | // Corresponding specification p.1548
|
|---|
| 190 | int pthread_attr_getstack(const pthread_attr_t *attr, void ** stackaddr, size_t *stacksize)
|
|---|
| 191 | {
|
|---|
| 192 | *stackaddr = attr->stackaddr;
|
|---|
| 193 | *stacksize = attr->stacksize;
|
|---|
| 194 | return 0;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | /**
|
|---|
| 198 | * Sets the guardsize field of the attribute
|
|---|
| 199 | * Corresponding specification: p. 1537-9
|
|---|
| 200 | *
|
|---|
| 201 | * @param *attr
|
|---|
| 202 | * The attribute to have it's guardsize set
|
|---|
| 203 | * @param guardsize
|
|---|
| 204 | * The guardsize to which the attribute's guardsize is set
|
|---|
| 205 | * @return Returns 0 upon successful completion
|
|---|
| 206 | */
|
|---|
| 207 |
|
|---|
| 208 | int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
|
|---|
| 209 | {
|
|---|
| 210 | attr->guardsize = guardsize;
|
|---|
| 211 | return 0;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | /**
|
|---|
| 215 | * Stores the guardsize value in an alternate location
|
|---|
| 216 | * Corresponding specification: p. 1537-9
|
|---|
| 217 | *
|
|---|
| 218 | * @param *attr
|
|---|
| 219 | * The attribute whose guardsize is to be stored
|
|---|
| 220 | * @param *guardsize
|
|---|
| 221 | * The location at which the attribute's guardsize is to be stored
|
|---|
| 222 | * @return Returns 0 upon successful completion
|
|---|
| 223 | */
|
|---|
| 224 |
|
|---|
| 225 | int pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
|
|---|
| 226 | {
|
|---|
| 227 | *guardsize = attr->guardsize;
|
|---|
| 228 | return 0;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | /**
|
|---|
| 232 | * Sets the scheduling policy field of the attribute
|
|---|
| 233 | * Corresponding specification: p. 1544-45
|
|---|
| 234 | *
|
|---|
| 235 | * @param *attr
|
|---|
| 236 | * The attribute to have it's scheduling policy set
|
|---|
| 237 | * @param policy
|
|---|
| 238 | * The scheduling policy to which the attribute's scheduling policy is set
|
|---|
| 239 | * @return Returns 0 upon successful completion
|
|---|
| 240 | */
|
|---|
| 241 |
|
|---|
| 242 | int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
|
|---|
| 243 | {
|
|---|
| 244 | attr->schedpolicy = policy;
|
|---|
| 245 | return 0;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | /**
|
|---|
| 249 | * Stores the scheduling policy value in an alternate location
|
|---|
| 250 | * Corresponding specification: p. 1544-45
|
|---|
| 251 | *
|
|---|
| 252 | * @param *attr
|
|---|
| 253 | * The attribute whose scheduling policy is to be stored
|
|---|
| 254 | * @param *policy
|
|---|
| 255 | * The location at which the attribute's scheduling policy is to be stored
|
|---|
| 256 | * @return Returns 0 upon successful completion
|
|---|
| 257 | */
|
|---|
| 258 |
|
|---|
| 259 | int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
|
|---|
| 260 | {
|
|---|
| 261 | *policy = attr->schedpolicy;
|
|---|
| 262 | return 0;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | /**
|
|---|
| 266 | * Initializes an attribute with the default values defined for it by an implementation.
|
|---|
| 267 | * Corresponding specification: p. 1647-51
|
|---|
| 268 | *
|
|---|
| 269 | * @param *attr
|
|---|
| 270 | * The attribute to be initialized.
|
|---|
| 271 | * @return Returns 0 upon successful completion
|
|---|
| 272 | */
|
|---|
| 273 |
|
|---|
| 274 | int pthread_mutexattr_init(pthread_mutexattr_t *attr){
|
|---|
| 275 | attr->robust = 0;
|
|---|
| 276 | attr->pshared = 0;
|
|---|
| 277 | attr->protocol = 0;
|
|---|
| 278 | attr->type = 0;
|
|---|
| 279 | attr->prioceiling = 0;
|
|---|
| 280 | return 0;
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | /**
|
|---|
| 284 | * Uninitializes the specified attr variable.
|
|---|
| 285 | * Corresponding specification: p. 1647-51
|
|---|
| 286 | *
|
|---|
| 287 | * @param *attr
|
|---|
| 288 | * The attribute to be uninitialized.
|
|---|
| 289 | * @return Returns 0 upon successful completion
|
|---|
| 290 | */
|
|---|
| 291 |
|
|---|
| 292 | int pthread_mutexattr_destroy(pthread_mutexattr_t *attr){
|
|---|
| 293 | pthread_mutexattr_t blank;
|
|---|
| 294 |
|
|---|
| 295 | *attr = blank;
|
|---|
| 296 | return 0;
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | /**
|
|---|
| 300 | * Stores the robustness value in an alternate location
|
|---|
| 301 | * Corresponding specification: p. 1659-1660
|
|---|
| 302 | *
|
|---|
| 303 | * @param *attr
|
|---|
| 304 | * The attribute whose robustness is to be stored
|
|---|
| 305 | * @param *robust
|
|---|
| 306 | * The location at which the attribute's robustness is to be stored
|
|---|
| 307 | * @return Returns 0 upon successful completion
|
|---|
| 308 | */
|
|---|
| 309 |
|
|---|
| 310 | int pthread_mutexattr_getrobust(const pthread_mutexattr_t *attr, int *robust){
|
|---|
| 311 | *robust = attr->robust;
|
|---|
| 312 | return 0;
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | /**
|
|---|
| 316 | * Sets the robustness field of the attribute
|
|---|
| 317 | * Corresponding specification: p. 1659-1660
|
|---|
| 318 | *
|
|---|
| 319 | * @param *attr
|
|---|
| 320 | * The attribute to have it's robustness set
|
|---|
| 321 | * @param robust
|
|---|
| 322 | * The robustness to which the attribute's robustness is set
|
|---|
| 323 | * @return Returns 0 upon successful completion
|
|---|
| 324 | */
|
|---|
| 325 |
|
|---|
| 326 | int pthread_mutexattr_setrobust(pthread_mutexattr_t *attr, int robust){
|
|---|
| 327 | attr->robust = robust;
|
|---|
| 328 | return 0;
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | /**
|
|---|
| 332 | * Stores the process shared variable in an alternate location
|
|---|
| 333 | * Corresponding specification: p. 1657-8
|
|---|
| 334 | *
|
|---|
| 335 | * @param *attr
|
|---|
| 336 | * The attribute whose process shared variable is to be stored
|
|---|
| 337 | * @param *detachstate
|
|---|
| 338 | * The location at which the attribute's process shared variable is to be stored
|
|---|
| 339 | * @return Returns 0 upon successful completion
|
|---|
| 340 | */
|
|---|
| 341 |
|
|---|
| 342 | int pthread_mutexattr_getpshared(const pthread_mutexattr_t *attr, int *pshared){
|
|---|
| 343 | *pshared = attr->pshared;
|
|---|
| 344 | return 0;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | /**
|
|---|
| 348 | * Sets the process shared variable field of the attribute
|
|---|
| 349 | * Corresponding specification: p. 1657-8
|
|---|
| 350 | *
|
|---|
| 351 | * @param *attr
|
|---|
| 352 | * The attribute to have it's process shared variable set
|
|---|
| 353 | * @param detachstate
|
|---|
| 354 | * The process shared variable to which the attribute's process shared variable is set
|
|---|
| 355 | * @return Returns 0 upon successful completion
|
|---|
| 356 | */
|
|---|
| 357 |
|
|---|
| 358 | int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared){
|
|---|
| 359 | attr->pshared = pshared;
|
|---|
| 360 | return 0;
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| 363 | /**
|
|---|
| 364 | * Stores the protocol value in an alternate location
|
|---|
| 365 | * Corresponding specification: p. 1654-56
|
|---|
| 366 | *
|
|---|
| 367 | * @param *attr
|
|---|
| 368 | * The attribute whose protocol is to be stored
|
|---|
| 369 | * @param *detachstate
|
|---|
| 370 | * The location at which the attribute's protocol is to be stored
|
|---|
| 371 | * @return Returns 0 upon successful completion
|
|---|
| 372 | */
|
|---|
| 373 |
|
|---|
| 374 | int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *attr, int *protocol){
|
|---|
| 375 | *protocol = attr->protocol;
|
|---|
| 376 | return 0;
|
|---|
| 377 | }
|
|---|
| 378 |
|
|---|
| 379 | /**
|
|---|
| 380 | * Sets the protocol field of the attribute
|
|---|
| 381 | * Corresponding specification: p. 1654-56
|
|---|
| 382 | *
|
|---|
| 383 | * @param *attr
|
|---|
| 384 | * The protocol to have it's protocol set
|
|---|
| 385 | * @param detachstate
|
|---|
| 386 | * The protocol to which the attribute's protocol is set
|
|---|
| 387 | * @return Returns 0 upon successful completion
|
|---|
| 388 | */
|
|---|
| 389 | int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr, int protocol){
|
|---|
| 390 | attr->protocol = protocol;
|
|---|
| 391 | return 0;
|
|---|
| 392 | }
|
|---|
| 393 |
|
|---|
| 394 | /**
|
|---|
| 395 | * Stores the type value in an alternate location
|
|---|
| 396 | * Corresponding specification: p. 1709-10
|
|---|
| 397 | *
|
|---|
| 398 | * @param *attr
|
|---|
| 399 | * The attribute whose type is to be stored
|
|---|
| 400 | * @param *detachstate
|
|---|
| 401 | * The location at which the attribute's type is to be stored
|
|---|
| 402 | * @return Returns 0 upon successful completion
|
|---|
| 403 | */
|
|---|
| 404 | int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type){
|
|---|
| 405 | *type = attr->type;
|
|---|
| 406 | return 0;
|
|---|
| 407 | }
|
|---|
| 408 |
|
|---|
| 409 | /**
|
|---|
| 410 | * Sets the type field of the attribute
|
|---|
| 411 | * Corresponding specification: p. 1709-10
|
|---|
| 412 | *
|
|---|
| 413 | * @param *attr
|
|---|
| 414 | * The attribute to have it's type set
|
|---|
| 415 | * @param detachstate
|
|---|
| 416 | * The type to which the attribute's type is set
|
|---|
| 417 | * @return Returns 0 upon successful completion
|
|---|
| 418 | */
|
|---|
| 419 | int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type){
|
|---|
| 420 | attr->type = type;
|
|---|
| 421 | return 0;
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 | /**
|
|---|
| 425 | * Stores the priority ceiling value in an alternate location
|
|---|
| 426 | * Corresponding specification: p. 1700-1
|
|---|
| 427 | *
|
|---|
| 428 | * @param *attr
|
|---|
| 429 | * The attribute whose priority ceiling is to be stored
|
|---|
| 430 | * @param *detachstate
|
|---|
| 431 | * The location at which the attribute's priority ceiling is to be stored
|
|---|
| 432 | * @return Returns 0 upon successful completion
|
|---|
| 433 | */
|
|---|
| 434 |
|
|---|
| 435 | int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *attr, int *prioceiling){
|
|---|
| 436 | *prioceiling = attr->prioceiling;
|
|---|
| 437 | return 0;
|
|---|
| 438 | }
|
|---|
| 439 |
|
|---|
| 440 | /**
|
|---|
| 441 | * Sets the priority ceiling field of the attribute
|
|---|
| 442 | * Corresponding specification: p. 1700-1
|
|---|
| 443 | *
|
|---|
| 444 | * @param *attr
|
|---|
| 445 | * The attribute to have it's priority ceiling set
|
|---|
| 446 | * @param detachstate
|
|---|
| 447 | * The priority ceiling to which the attribute's priority ceiling is set
|
|---|
| 448 | * @return Returns 0 upon successful completion
|
|---|
| 449 | */
|
|---|
| 450 |
|
|---|
| 451 | int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr, int prioceiling){
|
|---|
| 452 | attr->prioceiling = prioceiling;
|
|---|
| 453 | return 0;
|
|---|
| 454 | }
|
|---|
| 455 |
|
|---|
| 456 |
|
|---|
| 457 | /**
|
|---|
| 458 | * Initializes a mutex with the default values defined for it by an implementation
|
|---|
| 459 | * or with the values defined by the mutex attribute parameter
|
|---|
| 460 | * Corresponding specification: p. 1676-81
|
|---|
| 461 | *
|
|---|
| 462 | * @param *mutex
|
|---|
| 463 | * The mutex to be initialized.
|
|---|
| 464 | * @param *attr
|
|---|
| 465 | * The mutex attribute which the mutex shall take as it's field. May also
|
|---|
| 466 | * be null for default values to be initialized.
|
|---|
| 467 | * @return Returns 0 upon successful completion
|
|---|
| 468 | */
|
|---|
| 469 |
|
|---|
| 470 | int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr){
|
|---|
| 471 | if(attr == NULL){
|
|---|
| 472 | mutex->attr = (pthread_mutexattr_t *)malloc(sizeof(pthread_mutexattr_t));
|
|---|
| 473 | mutex->attr->robust = 0;
|
|---|
| 474 | mutex->attr->pshared = 0;
|
|---|
| 475 | mutex->attr->protocol = 0;
|
|---|
| 476 | mutex->attr->type = PTHREAD_MUTEX_NORMAL;
|
|---|
| 477 | mutex->attr->prioceiling = 0;
|
|---|
| 478 | }
|
|---|
| 479 | else{
|
|---|
| 480 | mutex->attr = (pthread_mutexattr_t *)malloc(sizeof(pthread_mutexattr_t));
|
|---|
| 481 | mutex->attr = attr;
|
|---|
| 482 | }
|
|---|
| 483 | mutex->lock = 0;
|
|---|
| 484 | mutex->count = 0;
|
|---|
| 485 | mutex->owner = $proc_null;
|
|---|
| 486 | return 0;
|
|---|
| 487 | }
|
|---|
| 488 |
|
|---|
| 489 | /**
|
|---|
| 490 | * Uninitializes the specified mutex variable.
|
|---|
| 491 | * Corresponding specification: p. 1676-81
|
|---|
| 492 | *
|
|---|
| 493 | * @param *mutex
|
|---|
| 494 | * The mutex to be uninitialized.
|
|---|
| 495 | * @return Returns 0 upon successful completion
|
|---|
| 496 | */
|
|---|
| 497 |
|
|---|
| 498 | int pthread_mutex_destroy(pthread_mutex_t *mutex){
|
|---|
| 499 | pthread_mutex_t blank;
|
|---|
| 500 |
|
|---|
| 501 | *mutex = blank;
|
|---|
| 502 | return 0;
|
|---|
| 503 | }
|
|---|
| 504 |
|
|---|
| 505 | /**
|
|---|
| 506 | * Initializes an condition with the default values defined for it by an implementation.
|
|---|
| 507 | * Corresponding specification: p. 1630-32
|
|---|
| 508 | *
|
|---|
| 509 | * @param *cond
|
|---|
| 510 | * The condition to be initialized.
|
|---|
| 511 | * @param *arg
|
|---|
| 512 | * Should be changed to condition attribute
|
|---|
| 513 | * @return Returns 0 upon successful completion
|
|---|
| 514 | */
|
|---|
| 515 |
|
|---|
| 516 | int pthread_cond_init(pthread_cond_t *cond, void *arg){
|
|---|
| 517 | cond->proccount = 0;
|
|---|
| 518 | cond->signal = 0;
|
|---|
| 519 | return 0;
|
|---|
| 520 | }
|
|---|
| 521 |
|
|---|
| 522 |
|
|---|
| 523 | /**
|
|---|
| 524 | * Uninitializes the specified cond variable.
|
|---|
| 525 | * Corresponding specification: p. 1630-2
|
|---|
| 526 | *
|
|---|
| 527 | * @param *cond
|
|---|
| 528 | * The condition to be uninitialized.
|
|---|
| 529 | * @return Returns 0 upon successful completion
|
|---|
| 530 | */
|
|---|
| 531 |
|
|---|
| 532 | int pthread_cond_destroy(pthread_cond_t *cond){
|
|---|
| 533 | if(cond->proccount != 0){
|
|---|
| 534 | $assert($false, "ERROR: Threads still waiting on specified condition variable");
|
|---|
| 535 | }
|
|---|
| 536 | else{
|
|---|
| 537 | pthread_cond_t blank;
|
|---|
| 538 | *cond = blank;
|
|---|
| 539 | }
|
|---|
| 540 | return 0;
|
|---|
| 541 | }
|
|---|
| 542 |
|
|---|
| 543 | /**
|
|---|
| 544 | * Defines a pthread_t by assigning it an attribute value (by value so the original attribute's state is
|
|---|
| 545 | * irrelevant), and spawning a process as the thr field with arguments void *arg
|
|---|
| 546 | * Corresponding specification: p. 1649-51
|
|---|
| 547 | *
|
|---|
| 548 | * @param *thread
|
|---|
| 549 | * The thread to be created with fields set from the other parameters.
|
|---|
| 550 | * @param *attr
|
|---|
| 551 | * The attribute to be assigned to the thread
|
|---|
| 552 | * @param *startroutine
|
|---|
| 553 | * The process to be spawned as the thread's actual 'thread'
|
|---|
| 554 | * @param *arg
|
|---|
| 555 | * The argument to be passed to the spawned function
|
|---|
| 556 | *
|
|---|
| 557 | * @return Returns 0 upon successful completion
|
|---|
| 558 | */
|
|---|
| 559 |
|
|---|
| 560 | int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg){
|
|---|
| 561 | thread->thr = $spawn start_routine(arg);
|
|---|
| 562 | if(attr == NULL){
|
|---|
| 563 | thread->attr = (pthread_attr_t *)malloc(sizeof(pthread_attr_t));
|
|---|
| 564 | thread->attr->stackaddr = NULL;
|
|---|
| 565 | thread->attr->stacksize = 0;
|
|---|
| 566 | thread->attr->guardsize = 0;
|
|---|
| 567 | thread->attr->detachstate = 1;
|
|---|
| 568 | thread->attr->inheritsched = 0;
|
|---|
| 569 | thread->attr->contentionscope = 0;
|
|---|
| 570 | thread->attr->schedpolicy = 0;
|
|---|
| 571 | }
|
|---|
| 572 | else{
|
|---|
| 573 | thread->attr = (pthread_attr_t *)malloc(sizeof(pthread_attr_t));
|
|---|
| 574 | thread->attr->stackaddr = attr->stackaddr;
|
|---|
| 575 | thread->attr->stacksize = attr->stacksize;
|
|---|
| 576 | thread->attr->guardsize = attr->guardsize;
|
|---|
| 577 | thread->attr->detachstate = attr->detachstate;
|
|---|
| 578 | thread->attr->inheritsched = attr->inheritsched;
|
|---|
| 579 | thread->attr->contentionscope = attr->contentionscope;
|
|---|
| 580 | thread->attr->schedpolicy = attr->schedpolicy;
|
|---|
| 581 | }
|
|---|
| 582 | _add_thread(&_pool, thread);
|
|---|
| 583 | return 0;
|
|---|
| 584 | }
|
|---|
| 585 |
|
|---|
| 586 | /**
|
|---|
| 587 | * Causes current thread to wait on thread specified as a parameter. If specified thread's detachstate field is set as PTHREAD_CREATE_DETACHED,
|
|---|
| 588 | * error will be returned stating the the thread cannot be joined. The value_ptr of pthread_exit shall be passed to any joining thread to the
|
|---|
| 589 | * terminated thread using pthread_join's value_ptr
|
|---|
| 590 | * Corresponding specification: p. 1617-9
|
|---|
| 591 | *
|
|---|
| 592 | * @param thread
|
|---|
| 593 | * The thread to be waited on by the current thread.
|
|---|
| 594 | * @param **value_ptr
|
|---|
| 595 | * The location at which the pthread_exit output is accessible
|
|---|
| 596 | *
|
|---|
| 597 | * @return Returns 0 upon successful completion
|
|---|
| 598 | */
|
|---|
| 599 |
|
|---|
| 600 | int pthread_join(pthread_t thread, void **value_ptr){
|
|---|
| 601 | if(thread.attr != NULL){
|
|---|
| 602 | if(thread.attr->detachstate == 0){
|
|---|
| 603 | $assert($false, "Thread is designated as unjoinable");
|
|---|
| 604 | return 1;
|
|---|
| 605 | }
|
|---|
| 606 | }
|
|---|
| 607 | $wait(thread.thr);
|
|---|
| 608 | if(value_ptr!=NULL)
|
|---|
| 609 | value_ptr = &value;
|
|---|
| 610 | return 0;
|
|---|
| 611 | }
|
|---|
| 612 |
|
|---|
| 613 | /**
|
|---|
| 614 | * Causes current thread to immediately terminate; if currently in the main method as specified by the
|
|---|
| 615 | * isMain parameter, the main method will wait for each thread to terminate before it terminates. The value
|
|---|
| 616 | * value_ptr will be made accessible in the location stated in pthread_join
|
|---|
| 617 | * Corresponding specification: p. 1655-6
|
|---|
| 618 | *
|
|---|
| 619 | * @param *value_ptr
|
|---|
| 620 | * The value to be stored in the location stated by pthread_join
|
|---|
| 621 | * @param isMain
|
|---|
| 622 | * Is this thread the main thread?
|
|---|
| 623 | * @param *arr
|
|---|
| 624 | * The array of threads which need to be waited upon by the main thread
|
|---|
| 625 | * @param len
|
|---|
| 626 | * The length of the array of threads to be waited upon
|
|---|
| 627 | * @return Returns 0 upon successful completion
|
|---|
| 628 | */
|
|---|
| 629 |
|
|---|
| 630 | int pthread_exit(void *value_ptr, _Bool isMain){
|
|---|
| 631 | if(isMain){
|
|---|
| 632 | for(int i = 0; i<_pool.len; i++)
|
|---|
| 633 | if($proc_defined(_pool.threads[i]->thr))
|
|---|
| 634 | $wait(_pool.threads[i]->thr);
|
|---|
| 635 | $exit();
|
|---|
| 636 | return 0;
|
|---|
| 637 | }
|
|---|
| 638 | else{
|
|---|
| 639 | value = value_ptr;
|
|---|
| 640 | $exit();
|
|---|
| 641 | return 0;
|
|---|
| 642 | }
|
|---|
| 643 | }
|
|---|
| 644 |
|
|---|
| 645 | //Unimplemented
|
|---|
| 646 | int pthread_detach(pthread_t thread);
|
|---|
| 647 |
|
|---|
| 648 | /**
|
|---|
| 649 | * Takes in a mutex variable and acts accordingly to its current state and type
|
|---|
| 650 | * PTHREAD_MUTEX_NORMAL: Checks to see whether mutex is already locked and behaves accordingly
|
|---|
| 651 | * locked and owner: Relock error, returns 0
|
|---|
| 652 | * locked and not owner: Waits until mutex is unlocked and then locks and becomes owner
|
|---|
| 653 | * unlocked and not owner: Locks the mutex and becomes owner
|
|---|
| 654 | * PTHREAD_MUTEX_RECURSIVE: A recursive mutex increments its count when it is locked and decremented when
|
|---|
| 655 | * it is unlocked and the lock is released when the count reaches 0.
|
|---|
| 656 | * PTHREAD_MUTEX_ERRORCHECK: Implemented similarly to PTHREAD_MUTEX_NORMAL, but notifies the user of errors
|
|---|
| 657 | * Corresponding specification: p. 1686-9
|
|---|
| 658 | *
|
|---|
| 659 | * @param *mutex
|
|---|
| 660 | * The mutex to be locked
|
|---|
| 661 | * @return Returns 0 upon successful completion, EOWNERDEAD upon termination of owner,
|
|---|
| 662 | */
|
|---|
| 663 |
|
|---|
| 664 | /*
|
|---|
| 665 | int pthread_mutex_lock(pthread_mutex_t *mutex){
|
|---|
| 666 | $atomic{
|
|---|
| 667 | if (mutex->attr->type == PTHREAD_MUTEX_NORMAL){
|
|---|
| 668 | if (mutex->lock != 0) {
|
|---|
| 669 | if(mutex->owner == $proc_null){
|
|---|
| 670 | if(mutex->attr->robust == PTHREAD_MUTEX_ROBUST){
|
|---|
| 671 | $when(mutex->lock == 0);
|
|---|
| 672 | }
|
|---|
| 673 | else{
|
|---|
| 674 | $assert($false);
|
|---|
| 675 | return EOWNERDEAD;
|
|---|
| 676 | }
|
|---|
| 677 | }
|
|---|
| 678 | else{
|
|---|
| 679 | if(mutex->owner == $self){
|
|---|
| 680 | $assert($false);
|
|---|
| 681 | return 0;
|
|---|
| 682 | }
|
|---|
| 683 | else{
|
|---|
| 684 | $when(mutex->lock == 0);
|
|---|
| 685 | }
|
|---|
| 686 | }
|
|---|
| 687 | }
|
|---|
| 688 | $atomic{
|
|---|
| 689 | $when(mutex->lock==0);
|
|---|
| 690 | mutex->lock = 1;
|
|---|
| 691 | mutex->owner = $self;
|
|---|
| 692 | }
|
|---|
| 693 | }
|
|---|
| 694 | else {
|
|---|
| 695 | int tmp = mutex->lock;
|
|---|
| 696 |
|
|---|
| 697 | mutex->lock = 1;
|
|---|
| 698 | if (tmp == 0) { // Attempts lock and checks for whether lock is already locked
|
|---|
| 699 | mutex->count = 1;
|
|---|
| 700 | mutex->owner = $self;
|
|---|
| 701 | }
|
|---|
| 702 | else {
|
|---|
| 703 | //Checks for ownership, otherwise returns error
|
|---|
| 704 | if(mutex->owner == $self){
|
|---|
| 705 | // Checks for recursive mutex, otherwise returns an error
|
|---|
| 706 | if (mutex->attr->type == PTHREAD_MUTEX_RECURSIVE) {
|
|---|
| 707 | mutex->count++;
|
|---|
| 708 | }
|
|---|
| 709 | else {
|
|---|
| 710 | $assert($false);
|
|---|
| 711 | return 0;
|
|---|
| 712 | }
|
|---|
| 713 | }
|
|---|
| 714 | else {
|
|---|
| 715 | $assert($false, "ERROR: Relock attempted on non-recursive mutex\n");
|
|---|
| 716 | return 0;
|
|---|
| 717 | }
|
|---|
| 718 | }
|
|---|
| 719 | }
|
|---|
| 720 | return 0;
|
|---|
| 721 | }
|
|---|
| 722 | }
|
|---|
| 723 | */
|
|---|
| 724 | /**
|
|---|
| 725 | * Takes in a mutex variable and acts similarly to pthread_mutex_lock except that
|
|---|
| 726 | * if the mutex is locked, it will return immeditately. In the case of a recursive mutex, the count will
|
|---|
| 727 | * be incremented and will return successfully.
|
|---|
| 728 | * Corresponding specification: p. 1686-9
|
|---|
| 729 | *
|
|---|
| 730 | * @param *mutex
|
|---|
| 731 | * The mutex to be locked
|
|---|
| 732 | * @return Returns 0 upon successful completion
|
|---|
| 733 | */
|
|---|
| 734 |
|
|---|
| 735 | int pthread_mutex_trylock(pthread_mutex_t *mutex){
|
|---|
| 736 | $atomic{
|
|---|
| 737 | if (mutex->attr->type == PTHREAD_MUTEX_NORMAL){
|
|---|
| 738 | if (mutex->lock != 0) {
|
|---|
| 739 | return EBUSY;
|
|---|
| 740 | }
|
|---|
| 741 | mutex->owner = $self;
|
|---|
| 742 | mutex->lock = 1;
|
|---|
| 743 | }
|
|---|
| 744 | else {
|
|---|
| 745 | int tmp = mutex->lock;
|
|---|
| 746 |
|
|---|
| 747 | mutex->lock = 1;
|
|---|
| 748 | if (tmp == 0) { // Attempts lock and checks for whether lock is already locked
|
|---|
| 749 | mutex->count = 1;
|
|---|
| 750 | mutex->owner = $self;
|
|---|
| 751 | }
|
|---|
| 752 | else {
|
|---|
| 753 | //Checks for ownership, otherwise returns error
|
|---|
| 754 | if(mutex->owner == $self){
|
|---|
| 755 | // Checks for recursive mutex, otherwise returns an error
|
|---|
| 756 | if (mutex->attr->type == PTHREAD_MUTEX_RECURSIVE) {
|
|---|
| 757 | mutex->count++;
|
|---|
| 758 | }
|
|---|
| 759 | else {
|
|---|
| 760 | $assert($false);
|
|---|
| 761 | return 0;
|
|---|
| 762 | }
|
|---|
| 763 | }
|
|---|
| 764 | else {
|
|---|
| 765 | return EBUSY;
|
|---|
| 766 | }
|
|---|
| 767 | }
|
|---|
| 768 | }
|
|---|
| 769 | return 0;
|
|---|
| 770 | }
|
|---|
| 771 | }
|
|---|
| 772 |
|
|---|
| 773 | /**
|
|---|
| 774 | * Takes in a mutex variable and acts accordingly to its current state and type
|
|---|
| 775 | * PTHREAD_MUTEX_NORMAL: Checks to see whether mutex is already unlocked and behaves accordingly
|
|---|
| 776 | * unlocked: returns error
|
|---|
| 777 | * locked: unlocks
|
|---|
| 778 | * PTHREAD_MUTEX_RECURSIVE: A recursive mutex increments its count when it is locked and decremented when
|
|---|
| 779 | * it is unlocked and the lock is released when the count reaches 0.
|
|---|
| 780 | * PTHREAD_MUTEX_ERRORCHECK: Currently implemented similarly to PTHREAD_MUTEX_NORMAL
|
|---|
| 781 | * Corresponding specification: p. 1686-9
|
|---|
| 782 | *
|
|---|
| 783 | * @param *mutex
|
|---|
| 784 | * The mutex to be unlocked
|
|---|
| 785 | * @return Returns 0 upon successful completion
|
|---|
| 786 | */
|
|---|
| 787 |
|
|---|
| 788 | int pthread_mutex_unlock(pthread_mutex_t *mutex){
|
|---|
| 789 | $atomic{
|
|---|
| 790 | if (mutex->attr->type == 0 || mutex->attr->type == 2) {
|
|---|
| 791 | int idx;
|
|---|
| 792 |
|
|---|
| 793 | // Attempts unlock, if already unlocked, returns error
|
|---|
| 794 | if (mutex->lock == 0) {
|
|---|
| 795 | $assert($false, "Attempting to unlock unlocked lock\n");
|
|---|
| 796 | return 0;
|
|---|
| 797 | }
|
|---|
| 798 | else {
|
|---|
| 799 | mutex->lock = 0;
|
|---|
| 800 | mutex->owner = $proc_null;
|
|---|
| 801 | }
|
|---|
| 802 | }
|
|---|
| 803 | else {
|
|---|
| 804 | //Checks for ownership of thread, if not, returns error
|
|---|
| 805 | if(mutex->owner == $self)
|
|---|
| 806 | {
|
|---|
| 807 | //Checks for recursive mutex
|
|---|
| 808 | _Bool tmp = !(mutex->attr->type == 1);
|
|---|
| 809 |
|
|---|
| 810 | if (--mutex->count == 0){
|
|---|
| 811 | mutex->lock = 0;
|
|---|
| 812 | mutex->owner = $proc_null;
|
|---|
| 813 | }
|
|---|
| 814 | }
|
|---|
| 815 | else {
|
|---|
| 816 | $assert($false);
|
|---|
| 817 | return 0;
|
|---|
| 818 | }
|
|---|
| 819 | }
|
|---|
| 820 | return 0;
|
|---|
| 821 | }
|
|---|
| 822 | }
|
|---|
| 823 |
|
|---|
| 824 | /**
|
|---|
| 825 | * Checks for robustness of mutex: if robust, the mutex is unlocked, otherwise an error is caused
|
|---|
| 826 | * and EINVAL is returned
|
|---|
| 827 | * Corresponding specification: p. 1674-5
|
|---|
| 828 | *
|
|---|
| 829 | * @param *mutex
|
|---|
| 830 | * The mutex to be marked as consistent
|
|---|
| 831 | * @return Returns 0 upon successful completion, EINVAL upon non-robust mutex input
|
|---|
| 832 | */
|
|---|
| 833 |
|
|---|
| 834 | int pthread_mutex_consistent(pthread_mutex_t *mutex){
|
|---|
| 835 | if(mutex->attr->robust == PTHREAD_MUTEX_ROBUST){
|
|---|
| 836 | mutex->lock = 0;
|
|---|
| 837 | return 0;
|
|---|
| 838 | }
|
|---|
| 839 | $assert($false);
|
|---|
| 840 | return EINVAL;
|
|---|
| 841 | }
|
|---|
| 842 |
|
|---|
| 843 | /**
|
|---|
| 844 | * Checks for calling thread as owner of the mutex, then increments proccount, unlocks the mutex
|
|---|
| 845 | * and sleeps. Awakens upon signal and decrements proccount and locks mutex.
|
|---|
| 846 | * Corresponding specification: p. 1634-9
|
|---|
| 847 | *
|
|---|
| 848 | * @param *cond
|
|---|
| 849 | * The condition to be waited upon until a signal is given
|
|---|
| 850 | * @param *mutex
|
|---|
| 851 | * The mutex used to lock other threads out
|
|---|
| 852 | * @return Returns 0 upon successful completion, EINVAL upon non-robust mutex input
|
|---|
| 853 | */
|
|---|
| 854 |
|
|---|
| 855 | int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex){
|
|---|
| 856 | if(mutex->owner != $self){
|
|---|
| 857 | printf("Mutex not owned by thread");
|
|---|
| 858 | $assert($false);
|
|---|
| 859 | return 0;
|
|---|
| 860 | }
|
|---|
| 861 |
|
|---|
| 862 | cond->proccount= cond->proccount+1;
|
|---|
| 863 | pthread_mutex_unlock(mutex);
|
|---|
| 864 |
|
|---|
| 865 | $when(cond->signal);
|
|---|
| 866 | cond->signal = false;
|
|---|
| 867 | --cond->proccount;
|
|---|
| 868 | $when(mutex->lock == 0){pthread_mutex_lock(mutex);}
|
|---|
| 869 | return 0;
|
|---|
| 870 | }
|
|---|
| 871 |
|
|---|
| 872 | /**
|
|---|
| 873 | * Signals the condition by setting the signal to true
|
|---|
| 874 | * Corresponding specification: p. 1627-30
|
|---|
| 875 | *
|
|---|
| 876 | * @param *cond
|
|---|
| 877 | * The condition to be signalled
|
|---|
| 878 | * @return Returns 0 upon successful completion
|
|---|
| 879 | */
|
|---|
| 880 |
|
|---|
| 881 | int pthread_cond_signal(pthread_cond_t *cond){
|
|---|
| 882 | cond->signal = true;
|
|---|
| 883 | return 0;
|
|---|
| 884 | }
|
|---|
| 885 |
|
|---|
| 886 | /**
|
|---|
| 887 | * Repeated signals the condition until all processes waiting have been signalled and awoken
|
|---|
| 888 | * Corresponding specification: p. 1627-30
|
|---|
| 889 | *
|
|---|
| 890 | * @param *cond
|
|---|
| 891 | * The condition to be signalled repeatedly
|
|---|
| 892 | * @return Returns 0 upon successful completion
|
|---|
| 893 | */
|
|---|
| 894 |
|
|---|
| 895 | int pthread_cond_broadcast(pthread_cond_t *cond){
|
|---|
| 896 | while(cond->proccount > 0){
|
|---|
| 897 | cond->signal = true;
|
|---|
| 898 | }
|
|---|
| 899 | return 0;
|
|---|
| 900 | }
|
|---|