OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [test/] [pthread/] [ex7.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/* ex7
2
 *
3
 * Test case that illustrates a timed wait on a condition variable.
4
 */
5
 
6
#include <errno.h>
7
#include <stdio.h>
8
#include <string.h>
9
#include <pthread.h>
10
#include <sys/time.h>
11
#include <unistd.h>
12
 
13
/* Our event variable using a condition variable contruct. */
14
typedef struct {
15
    pthread_mutex_t     mutex;
16
    pthread_cond_t      cond;
17
    int                 flag;
18
} event_t;
19
 
20
 
21
/* Global event to signal main thread the timeout of the child thread. */
22
event_t main_event;
23
 
24
 
25
void *
26
test_thread (void *ms_param)
27
{
28
    int status = 0;
29
    event_t foo;
30
    struct timespec time;
31
    struct timeval  now;
32
    long ms = (long) ms_param;
33
 
34
    /* initialize cond var */
35
    pthread_cond_init(&foo.cond, NULL);
36
    pthread_mutex_init(&foo.mutex, NULL);
37
    foo.flag = 0;
38
 
39
    /* set the time out value */
40
    printf("waiting %ld ms ...\n", ms);
41
    gettimeofday(&now, NULL);
42
    time.tv_sec  = now.tv_sec + ms/1000 + (now.tv_usec + (ms%1000)*1000)/1000000;
43
    time.tv_nsec = ((now.tv_usec + (ms%1000)*1000) % 1000000) * 1000;
44
 
45
    /* Just use this to test the time out. The cond var is never signaled. */
46
    pthread_mutex_lock(&foo.mutex);
47
    while (foo.flag == 0 && status != ETIMEDOUT) {
48
        status = pthread_cond_timedwait(&foo.cond, &foo.mutex, &time);
49
    }
50
    pthread_mutex_unlock(&foo.mutex);
51
 
52
    /* post the main event */
53
    pthread_mutex_lock(&main_event.mutex);
54
    main_event.flag = 1;
55
    pthread_cond_signal(&main_event.cond);
56
    pthread_mutex_unlock(&main_event.mutex);
57
 
58
    /* that's it, bye */
59
    return (void*) status;
60
}
61
 
62
int
63
main (void)
64
{
65
  unsigned long count;
66
 
67
  setvbuf (stdout, NULL, _IONBF, 0);
68
 
69
  /* initialize main event cond var */
70
  pthread_cond_init(&main_event.cond, NULL);
71
  pthread_mutex_init(&main_event.mutex, NULL);
72
  main_event.flag = 0;
73
 
74
  for (count = 0; count < 20; ++count)
75
  {
76
      pthread_t thread;
77
      int status;
78
 
79
      /* pass down the milli-second timeout in the void* param */
80
      status = pthread_create (&thread, NULL, test_thread, (void*) (count*100));
81
      if (status != 0) {
82
          printf ("status = %d, count = %lu: %s\n", status, count,
83
                  strerror (errno));
84
          return 1;
85
      }
86
      else {
87
 
88
          /* wait for the event posted by the child thread */
89
          pthread_mutex_lock(&main_event.mutex);
90
          while (main_event.flag == 0) {
91
              pthread_cond_wait(&main_event.cond, &main_event.mutex);
92
          }
93
          main_event.flag = 0;
94
          pthread_mutex_unlock(&main_event.mutex);
95
 
96
          printf ("count = %lu\n", count);
97
      }
98
 
99
      usleep (10);
100
  }
101
 
102
  return 0;
103
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.