| 1 | /*****************************************************************************
|
|---|
| 2 | * SOURCE: This is a translation of a Pthread program from the Lawrence Livermore
|
|---|
| 3 | * Computing Center POSIX Threads Programming Exercise at:
|
|---|
| 4 | * https://computing.llnl.gov/tutorials/pthreads/exercise.html
|
|---|
| 5 | * SUPERSOURCE: Adapted from example code in "Pthreads Programming", B. Nichols
|
|---|
| 6 | * et al. O'Reilly and Associates.
|
|---|
| 7 | * FILE: bug1fix.cvl
|
|---|
| 8 | * DESCRIPTION:
|
|---|
| 9 | * Solution for the bug1.cvl program. The inc_count routine uses a
|
|---|
| 10 | * pthread_cond_broadcast() routine instead of the pthread_cond_signal()
|
|---|
| 11 | * routine.
|
|---|
| 12 | * Command line execution:
|
|---|
| 13 | * civl verify -inputNUM_THREADS=6 -inputTCOUNT=10 -inputCOUNT_LIMIT=12 bug1fix.cvl
|
|---|
| 14 | ******************************************************************************/
|
|---|
| 15 |
|
|---|
| 16 | #include "pthread.cvh"
|
|---|
| 17 | #include <civlc.h>
|
|---|
| 18 | #include <stdio.h>
|
|---|
| 19 | #include <stdlib.h>
|
|---|
| 20 | #include "math.cvh"
|
|---|
| 21 |
|
|---|
| 22 | $input int NUM_THREADS; //Changed definitions to input variables
|
|---|
| 23 | $input int TCOUNT;
|
|---|
| 24 | $input int COUNT_LIMIT;
|
|---|
| 25 |
|
|---|
| 26 | int count = 0;
|
|---|
| 27 | pthread_mutex_t count_mutex;
|
|---|
| 28 | pthread_cond_t count_threshold_cv;
|
|---|
| 29 |
|
|---|
| 30 | void *inc_count(void *idp)
|
|---|
| 31 | {
|
|---|
| 32 | int j,i;
|
|---|
| 33 | double result=0.0;
|
|---|
| 34 | long my_id = (long)*idp; //Changed to dereference, can't directly convert from void* to long
|
|---|
| 35 |
|
|---|
| 36 | for (i=0; i < TCOUNT; i++) {
|
|---|
| 37 | pthread_mutex_lock(&count_mutex);
|
|---|
| 38 | count++;
|
|---|
| 39 |
|
|---|
| 40 | /*
|
|---|
| 41 | Check the value of count and signal waiting thread when condition is
|
|---|
| 42 | reached. Note that this occurs while mutex is locked.
|
|---|
| 43 | */
|
|---|
| 44 | if (count == COUNT_LIMIT) {
|
|---|
| 45 | pthread_cond_broadcast(&count_threshold_cv);
|
|---|
| 46 | // Removed l from %ld, unsupported
|
|---|
| 47 | printf("inc_count(): thread %d, count = %d Threshold reached.\n", my_id, count);
|
|---|
| 48 | }
|
|---|
| 49 | // Removed l from %ld, unsupported
|
|---|
| 50 | printf("inc_count(): thread %d, count = %d, unlocking mutex\n", my_id, count);
|
|---|
| 51 | pthread_mutex_unlock(&count_mutex);
|
|---|
| 52 |
|
|---|
| 53 | /* Do some work so threads can alternate on mutex lock */
|
|---|
| 54 | //sleep(1); Removed sleep, unnecessary
|
|---|
| 55 | pthread_exit(NULL, false, NULL, 0);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | void *watch_count(void *idp)
|
|---|
| 59 | {
|
|---|
| 60 | long my_id = (long)*idp;
|
|---|
| 61 |
|
|---|
| 62 | // Removed l from %ld, unsupported
|
|---|
| 63 | printf("Starting watch_count(): thread %d\n", my_id);
|
|---|
| 64 |
|
|---|
| 65 | /*
|
|---|
| 66 | Lock mutex and wait for signal. Note that the pthread_cond_wait routine
|
|---|
| 67 | will automatically and atomically unlock mutex while it waits.
|
|---|
| 68 | Also, note that if COUNT_LIMIT is reached before this routine is run by
|
|---|
| 69 | the waiting thread, the loop will be skipped to prevent pthread_cond_wait
|
|---|
| 70 | from never returning.
|
|---|
| 71 | */
|
|---|
| 72 | pthread_mutex_lock(&count_mutex);
|
|---|
| 73 | printf("***Before cond_wait: thread %d\n", my_id); // Removed l from %ld, unsupported
|
|---|
| 74 | pthread_cond_wait(&count_threshold_cv, &count_mutex);
|
|---|
| 75 | printf("***Thread %d Condition signal received.\n", my_id); // Removed l from %ld, unsupported
|
|---|
| 76 | pthread_mutex_unlock(&count_mutex);
|
|---|
| 77 | pthread_exit(NULL, false, NULL, 0); //Different parameters
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | int main(void)
|
|---|
| 81 | {
|
|---|
| 82 | int i, rc;
|
|---|
| 83 | pthread_t threads[6];
|
|---|
| 84 | pthread_attr_t attr;
|
|---|
| 85 |
|
|---|
| 86 | /* Initialize mutex and condition variable objects */
|
|---|
| 87 | pthread_mutex_init(&count_mutex, NULL);
|
|---|
| 88 | pthread_cond_init (&count_threshold_cv, NULL);
|
|---|
| 89 |
|
|---|
| 90 | /*
|
|---|
| 91 | For portability, explicitly create threads in a joinable state
|
|---|
| 92 | Created variables for casting as we are unable to currently cast raw integer values as
|
|---|
| 93 | void pointers.
|
|---|
| 94 | */
|
|---|
| 95 | int zero = 0;
|
|---|
| 96 | int one = 1;
|
|---|
| 97 | int two = 2;
|
|---|
| 98 | int three = 3;
|
|---|
| 99 | int four = 4;
|
|---|
| 100 | int five = 5;
|
|---|
| 101 | pthread_attr_init(&attr);
|
|---|
| 102 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
|---|
| 103 | pthread_create(&threads[2], &attr, watch_count, (void *)&two);
|
|---|
| 104 | pthread_create(&threads[3], &attr, watch_count, (void *)&three);
|
|---|
| 105 | pthread_create(&threads[4], &attr, watch_count, (void *)&four);
|
|---|
| 106 | pthread_create(&threads[5], &attr, watch_count, (void *)&five);
|
|---|
| 107 | pthread_create(&threads[0], &attr, inc_count, (void *)&zero);
|
|---|
| 108 | pthread_create(&threads[1], &attr, inc_count, (void *)&one);
|
|---|
| 109 |
|
|---|
| 110 | /* Wait for all threads to complete*/
|
|---|
| 111 | for (i = 0; i < NUM_THREADS; i++) {
|
|---|
| 112 | pthread_join(threads[i], NULL);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | printf ("Main(): Waited on %d threads. Done.\n", NUM_THREADS);
|
|---|
| 116 |
|
|---|
| 117 | /* Clean up and exit */
|
|---|
| 118 | pthread_attr_destroy(&attr);
|
|---|
| 119 | pthread_mutex_destroy(&count_mutex);
|
|---|
| 120 | pthread_cond_destroy(&count_threshold_cv);
|
|---|
| 121 | pthread_exit(NULL, true, NULL, 0); //Different parameters
|
|---|
| 122 |
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|