bool dequeue(queue_t *q, unsigned int *retVal) { std::string str1("dequeue"); //ANNOTATION function_call(str1, INVOCATION); //ANNOTATION int success = 0; pointer head; pointer tail; pointer next; while (!success) { head = atomic_load_explicit(&q->head, memory_order_seq_cst); tail = atomic_load_explicit(&q->tail, memory_order_seq_cst); next = atomic_load_explicit(&q->nodes[get_ptr(head)].next, memory_order_seq_cst); if (atomic_load_explicit(&q->head, memory_order_seq_cst) == head) { if (get_ptr(head) == get_ptr(tail)) { /* Check for uninitialized 'next' */ MODEL_ASSERT(get_ptr(next) != POISON_IDX); if (get_ptr(next) == 0) { // NULL function_call(str1, RESPONSE); //ANNOTATION return false; // NULL } atomic_compare_exchange_strong_explicit(&q->tail, &tail, MAKE_POINTER(get_ptr(next), get_count(tail) + 1), memory_order_seq_cst, memory_order_seq_cst); thrd_yield(); } else { *retVal = load_32(&q->nodes[get_ptr(next)].value); success = atomic_compare_exchange_strong_explicit(&q->head, &head, MAKE_POINTER(get_ptr(next), get_count(head) + 1), memory_order_seq_cst, memory_order_seq_cst); if (!success) thrd_yield(); } } } reclaim(get_ptr(head)); function_call(str1, RESPONSE, *retVal); //ANNOTATION return true; }
/* Thread function: Yield */ static int thread_yield(void * aArg) { (void)aArg; /* Yield... */ thrd_yield(); return 0; }
// 7.25.4.4 int mtx_timedlock(mtx_t *mtx, const xtime *xt) { time_t expire, now; if (!mtx || !xt) return thrd_error; expire = time(NULL); expire += xt->sec; while (mtx_trylock(mtx) != thrd_success) { now = time(NULL); if (expire < now) return thrd_busy; // busy loop! thrd_yield(); } return thrd_success; }
void enqueue(queue_t *q, unsigned int val) { std::string str1("enqueue"); //ANNOTATION function_call(str1, INVOCATION, val); //ANNOTATION int success = 0; unsigned int node; pointer tail; pointer next; pointer tmp; node = new_node(); store_32(&q->nodes[node].value, val); tmp = atomic_load_explicit(&q->nodes[node].next, memory_order_seq_cst); set_ptr(&tmp, 0); // NULL atomic_store_explicit(&q->nodes[node].next, tmp, memory_order_seq_cst); while (!success) { tail = atomic_load_explicit(&q->tail, memory_order_seq_cst); next = atomic_load_explicit(&q->nodes[get_ptr(tail)].next, memory_order_seq_cst); if (tail == atomic_load_explicit(&q->tail, memory_order_seq_cst)) { /* Check for uninitialized 'next' */ MODEL_ASSERT(get_ptr(next) != POISON_IDX); if (get_ptr(next) == 0) { // == NULL pointer value = MAKE_POINTER(node, get_count(next) + 1); success = atomic_compare_exchange_strong_explicit(&q->nodes[get_ptr(tail)].next, &next, value, memory_order_seq_cst, memory_order_seq_cst); } if (!success) { unsigned int ptr = get_ptr(atomic_load_explicit(&q->nodes[get_ptr(tail)].next, memory_order_seq_cst)); pointer value = MAKE_POINTER(ptr, get_count(tail) + 1); atomic_compare_exchange_strong_explicit(&q->tail, &tail, value, memory_order_seq_cst, memory_order_seq_cst); thrd_yield(); } } } atomic_compare_exchange_strong_explicit(&q->tail, &tail, MAKE_POINTER(node, get_count(tail) + 1), memory_order_seq_cst, memory_order_seq_cst); function_call(str1, RESPONSE); //ANNOTATION }
void reader(int* idPtr) { int id = *idPtr; while(1) { // Check semaphore; decrement and read if count > 0. while( atomic_fetch_sub(&count, 1) <= 0) { atomic_fetch_add(&count, 1); thrd_yield(); } if( data > 0) // Read valid data. printf("Reader %d is reading %d\n", id, data); if( data < 0) // End marker: stop looping. break; atomic_fetch_add(&count, 1); // Release our reader slot. thrd_sleep(&ms,NULL); // Simulate data processing. } }
static void test_yield (void) { thrd_t t[40]; int i; /* Start a bunch of child threads */ for (i = 0; i < 40; ++ i) { thrd_create(&t[i], thread_yield, NULL); } /* Yield... */ thrd_yield(); /* Wait for the threads to finish */ for (i = 0; i < 40; ++ i) { thrd_join(t[i], NULL); } }
// 7.25.2.1 void call_once(once_flag *flag, void (*func)(void)) { assert(flag && func); #ifdef EMULATED_THREADS_USE_NATIVE_CALL_ONCE { struct impl_call_once_param param; param.func = func; InitOnceExecuteOnce(flag, impl_call_once_callback, (PVOID)¶m, NULL); } #else if (InterlockedCompareExchange(&flag->status, 1, 0) == 0) { (func)(); InterlockedExchange(&flag->status, 2); } else { while (flag->status == 1) { // busy loop! thrd_yield(); } } #endif }
int main(void) { thrd_t th; if( thrd_create( &th, (thrd_start_t)thrdFunc, NULL) != thrd_success) { fprintf(stderr,"Thread-Fehler!\n"); return -1; } thrd_yield(); puts("main..............................................\n" ".................................................."); int n = atomic_load_explicit(&ai, memory_order_acquire); if( n > 0) { for( int i = 0; i < n; ++i) // Operation B printf("%8.2lf", data[i].x); putchar('\n'); } else printf("\nData not yet available.\n"); thrd_join(th, NULL); return 0; }
static mp_obj_t module_thrd_yield(void) { thrd_yield(); return (mp_const_none); }
/* This is the main program (i.e. the main thread) */ int main(void) { /* Initialization... */ mtx_init(&gMutex, mtx_plain); cnd_init(&gCond); /* Test 1: thread arguments & return values */ printf("PART I: Thread arguments & return values\n"); { thrd_t t1, t2, t3, t4; int id1, id2, id3, id4, ret1, ret2, ret3, ret4; /* Start a bunch of child threads */ id1 = 1; thrd_create(&t1, ThreadIDs, (void*)&id1); thrd_join(t1, &ret1); printf(" Thread 1 returned %d.\n", ret1); id2 = 2; thrd_create(&t2, ThreadIDs, (void*)&id2); thrd_join(t2, &ret2); printf(" Thread 2 returned %d.\n", ret2); id3 = 3; thrd_create(&t3, ThreadIDs, (void*)&id3); thrd_join(t3, &ret3); printf(" Thread 3 returned %d.\n", ret3); id4 = 4; thrd_create(&t4, ThreadIDs, (void*)&id4); thrd_join(t4, &ret4); printf(" Thread 4 returned %d.\n", ret4); } /* Test 2: compile time thread local storage */ printf("PART II: Compile time thread local storage\n"); #ifndef NO_CT_TLS { thrd_t t1; /* Clear the TLS variable (it should keep this value after all threads are finished). */ gLocalVar = 1; printf(" Main gLocalVar is %d.\n", gLocalVar); /* Start a child thread that modifies gLocalVar */ thrd_create(&t1, ThreadTLS, NULL); thrd_join(t1, NULL); /* Check if the TLS variable has changed */ if(gLocalVar == 1) printf(" Main gLocalID was not changed by the child thread - OK!\n"); else printf(" Main gLocalID was changed by the child thread - FAIL!\n"); } #else printf(" Compile time TLS is not supported on this platform...\n"); #endif /* Test 3: mutex locking */ printf("PART III: Mutex locking (100 threads x 10000 iterations)\n"); { thrd_t t[100]; int i; /* Clear the global counter. */ gCount = 0; /* Start a bunch of child threads */ for (i = 0; i < 100; ++ i) { thrd_create(&t[i], ThreadLock, NULL); } /* Wait for the threads to finish */ for (i = 0; i < 100; ++ i) { thrd_join(t[i], NULL); } /* Check the global count */ printf(" gCount = %d\n", gCount); } /* Test 4: condition variable */ printf("PART IV: Condition variable (40 + 1 threads)\n"); { thrd_t t1, t[40]; int i; /* Set the global counter to the number of threads to run. */ gCount = 40; /* Start the waiting thread (it will wait for gCount to reach zero). */ thrd_create(&t1, ThreadCondition2, NULL); /* Start a bunch of child threads (these will decrease gCount by 1 when they finish) */ for (i = 0; i < 40; ++ i) { thrd_create(&t[i], ThreadCondition1, NULL); } /* Wait for the waiting thread to finish */ thrd_join(t1, NULL); /* Wait for the other threads to finish */ for (i = 0; i < 40; ++ i) { thrd_join(t[i], NULL); } } /* Test 5: yield */ printf("PART V: Yield (40 + 1 threads)\n"); { thrd_t t[40]; int i; /* Start a bunch of child threads */ for (i = 0; i < 40; ++ i) { thrd_create(&t[i], ThreadYield, NULL); } /* Yield... */ thrd_yield(); /* Wait for the threads to finish */ for (i = 0; i < 40; ++ i) { thrd_join(t[i], NULL); } } /* Test 6: Sleep */ printf("PART VI: Sleep (10 x 100 ms)\n"); { int i; struct timespec ts; printf(" Sleeping"); fflush(stdout); for (i = 0; i < 10; ++ i) { /* Calculate current time + 100ms */ clock_gettime(TIME_UTC, &ts); ts.tv_nsec += 100000000; if (ts.tv_nsec >= 1000000000) { ts.tv_sec++; ts.tv_nsec -= 1000000000; } /* Sleep... */ thrd_sleep(&ts, NULL); printf("."); fflush(stdout); } printf("\n"); } /* Test 7: Time */ printf("PART VII: Current time (UTC), three times\n"); { struct timespec ts; clock_gettime(TIME_UTC, &ts); printf(" Time = %ld.%09ld\n", (long)ts.tv_sec, ts.tv_nsec); clock_gettime(TIME_UTC, &ts); printf(" Time = %ld.%09ld\n", (long)ts.tv_sec, ts.tv_nsec); clock_gettime(TIME_UTC, &ts); printf(" Time = %ld.%09ld\n", (long)ts.tv_sec, ts.tv_nsec); } /* FIXME: Implement some more tests for the TinyCThread API... */ /* Finalization... */ mtx_destroy(&gMutex); cnd_destroy(&gCond); return 0; }
/* Thread function: Yield */ int ThreadYield(void * aArg) { /* Yield... */ thrd_yield(); return 0; }
int test_yield(struct harness_t *harness_p) { BTASSERT(thrd_yield() == 0); return (0); }