| 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: bug1.cvl
|
|---|
| 8 | * DESCRIPTION:
|
|---|
| 9 | * This example has a bug. It is a variation on the condvar.cvl example.
|
|---|
| 10 | * Instead of just one thread waiting for the condition signal, there are
|
|---|
| 11 | * four threads waiting for the same signal causing a deadlock.
|
|---|
| 12 | * The solution program is bug1fix.cvl
|
|---|
| 13 | * Command line execution:
|
|---|
| 14 | * civl verify -inputNUM_THREADS=6 -inputTCOUNT=10 -inputCOUNT_LIMIT=12 bug1.cvl
|
|---|
| 15 | ******************************************************************************/
|
|---|
| 16 |
|
|---|
| 17 | #include "pthread.cvh"
|
|---|
| 18 | #include <civlc.h>
|
|---|
| 19 | #include <stdio.h>
|
|---|
| 20 | #include <stdlib.h>
|
|---|
| 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
|
|---|
| 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_signal(&count_threshold_cv);
|
|---|
| 46 | printf("inc_count(): thread %d, count = %d Threshold reached.\n", my_id, count); //removed l from %ld
|
|---|
| 47 | }
|
|---|
| 48 | printf("inc_count(): thread %d, count = %d, unlocking mutex\n", my_id, count); //removed l from %ld
|
|---|
| 49 | pthread_mutex_unlock(&count_mutex);
|
|---|
| 50 |
|
|---|
| 51 | /* Do some work so threads can alternate on mutex lock */
|
|---|
| 52 | //sleep(1); removed sleep, unnecessary
|
|---|
| 53 | }
|
|---|
| 54 | pthread_exit(NULL, false, NULL, 0);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | void *watch_count(void *idp)
|
|---|
| 58 | {
|
|---|
| 59 | long my_id = (long)*idp;
|
|---|
| 60 |
|
|---|
| 61 | printf("Starting watch_count(): thread %d\n", my_id);
|
|---|
| 62 |
|
|---|
| 63 | /*
|
|---|
| 64 | Lock mutex and wait for signal. Note that the pthread_cond_wait routine
|
|---|
| 65 | will automatically and atomically unlock mutex while it waits.
|
|---|
| 66 | Also, note that if COUNT_LIMIT is reached before this routine is run by
|
|---|
| 67 | the waiting thread, the loop will be skipped to prevent pthread_cond_wait
|
|---|
| 68 | from never returning.
|
|---|
| 69 | */
|
|---|
| 70 | pthread_mutex_lock(&count_mutex);
|
|---|
| 71 | printf("***Before cond_wait: thread %d\n", my_id);
|
|---|
| 72 | pthread_cond_wait(&count_threshold_cv, &count_mutex);
|
|---|
| 73 | printf("***Thread %d Condition signal received.\n", my_id);
|
|---|
| 74 | pthread_mutex_unlock(&count_mutex);
|
|---|
| 75 | pthread_exit(NULL, false, NULL, 0); //Different parameters
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | int main(void)
|
|---|
| 79 | {
|
|---|
| 80 | int i, rc;
|
|---|
| 81 | pthread_t threads[6];
|
|---|
| 82 | pthread_attr_t attr;
|
|---|
| 83 |
|
|---|
| 84 | /* Initialize mutex and condition variable objects */
|
|---|
| 85 | pthread_mutex_init(&count_mutex, 0);
|
|---|
| 86 | pthread_cond_init (&count_threshold_cv, NULL);
|
|---|
| 87 |
|
|---|
| 88 | /*
|
|---|
| 89 | For portability, explicitly create threads in a joinable state
|
|---|
| 90 | Created variables for casting as we are unable to currently cast raw integer values as
|
|---|
| 91 | void pointers.
|
|---|
| 92 | */
|
|---|
| 93 | int zero = 0;
|
|---|
| 94 | int one = 1;
|
|---|
| 95 | int two = 2;
|
|---|
| 96 | int three = 3;
|
|---|
| 97 | int four = 4;
|
|---|
| 98 | int five = 5;
|
|---|
| 99 | pthread_attr_init(&attr);
|
|---|
| 100 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
|---|
| 101 | pthread_create(&threads[2], &attr, watch_count, &two);
|
|---|
| 102 | pthread_create(&threads[3], &attr, watch_count, &three);
|
|---|
| 103 | pthread_create(&threads[4], &attr, watch_count, &four);
|
|---|
| 104 | pthread_create(&threads[5], &attr, watch_count, &five);
|
|---|
| 105 | pthread_create(&threads[0], &attr, inc_count, &zero);
|
|---|
| 106 | pthread_create(&threads[1], &attr, inc_count, &one);
|
|---|
| 107 |
|
|---|
| 108 | pthread_attr_destroy(&attr);
|
|---|
| 109 |
|
|---|
| 110 | /* Wait for all threads to complete */
|
|---|
| 111 | for (i = 0; i < NUM_THREADS; i++) {
|
|---|
| 112 | pthread_join(threads[i], NULL);
|
|---|
| 113 | }
|
|---|
| 114 | printf ("Main(): Waited on %d threads. Done.\n", NUM_THREADS);
|
|---|
| 115 |
|
|---|
| 116 | /* Clean up and exit */
|
|---|
| 117 |
|
|---|
| 118 | pthread_mutex_destroy(&count_mutex);
|
|---|
| 119 | pthread_cond_destroy(&count_threshold_cv);
|
|---|
| 120 | pthread_exit (NULL, true, NULL, 0); //Different parameters
|
|---|
| 121 | return 0;
|
|---|
| 122 | }
|
|---|