summaryrefslogtreecommitdiff
path: root/linuxthreads/Examples
diff options
context:
space:
mode:
authorAndreas Jaeger <aj@suse.de>2000-12-27 17:14:56 +0000
committerAndreas Jaeger <aj@suse.de>2000-12-27 17:14:56 +0000
commita375a533a2445079390907b962464a379d25f5d5 (patch)
treed781d06615c1d76d029c48e1d7eb69dc21ecbf3d /linuxthreads/Examples
parent21112857299d8a7b9c04fcb2027ae5186acfcf4f (diff)
* Examples/ex13.c: Make local functions static.
* ecmutex.c: Likewise. * Examples/ex14.c: Likewise. * Examples/ex2.c: Make local functions static; reformat. * Examples/ex1.c: Likewise. * Examples/ex4.c: Likewise. * Examples/ex5.c: Likewise. * Examples/ex7.c: Likewise. CVS ----------------------------------------------------------------------
Diffstat (limited to 'linuxthreads/Examples')
-rw-r--r--linuxthreads/Examples/ex1.c37
-rw-r--r--linuxthreads/Examples/ex13.c6
-rw-r--r--linuxthreads/Examples/ex14.c2
-rw-r--r--linuxthreads/Examples/ex2.c109
-rw-r--r--linuxthreads/Examples/ex4.c84
-rw-r--r--linuxthreads/Examples/ex5.c82
-rw-r--r--linuxthreads/Examples/ex7.c33
7 files changed, 198 insertions, 155 deletions
diff --git a/linuxthreads/Examples/ex1.c b/linuxthreads/Examples/ex1.c
index f455ecfaf0..29138cf761 100644
--- a/linuxthreads/Examples/ex1.c
+++ b/linuxthreads/Examples/ex1.c
@@ -7,29 +7,36 @@
#include <unistd.h>
#include "pthread.h"
-void * process(void * arg)
+static void *
+process (void *arg)
{
int i;
- fprintf(stderr, "Starting process %s\n", (char *) arg);
- for (i = 0; i < 10000; i++) {
- write(1, (char *) arg, 1);
- }
+ fprintf (stderr, "Starting process %s\n", (char *) arg);
+ for (i = 0; i < 10000; i++)
+ {
+ write (1, (char *) arg, 1);
+ }
return NULL;
}
-int main(void)
+int
+main (void)
{
int retcode;
pthread_t th_a, th_b;
- void * retval;
+ void *retval;
- retcode = pthread_create(&th_a, NULL, process, (void *) "a");
- if (retcode != 0) fprintf(stderr, "create a failed %d\n", retcode);
- retcode = pthread_create(&th_b, NULL, process, (void *) "b");
- if (retcode != 0) fprintf(stderr, "create b failed %d\n", retcode);
- retcode = pthread_join(th_a, &retval);
- if (retcode != 0) fprintf(stderr, "join a failed %d\n", retcode);
- retcode = pthread_join(th_b, &retval);
- if (retcode != 0) fprintf(stderr, "join b failed %d\n", retcode);
+ retcode = pthread_create (&th_a, NULL, process, (void *) "a");
+ if (retcode != 0)
+ fprintf (stderr, "create a failed %d\n", retcode);
+ retcode = pthread_create (&th_b, NULL, process, (void *) "b");
+ if (retcode != 0)
+ fprintf (stderr, "create b failed %d\n", retcode);
+ retcode = pthread_join (th_a, &retval);
+ if (retcode != 0)
+ fprintf (stderr, "join a failed %d\n", retcode);
+ retcode = pthread_join (th_b, &retval);
+ if (retcode != 0)
+ fprintf (stderr, "join b failed %d\n", retcode);
return 0;
}
diff --git a/linuxthreads/Examples/ex13.c b/linuxthreads/Examples/ex13.c
index 3a0347993a..af95358297 100644
--- a/linuxthreads/Examples/ex13.c
+++ b/linuxthreads/Examples/ex13.c
@@ -32,7 +32,7 @@ struct thr_ctrl
int retval;
};
-void
+static void
dump_mut (pthread_mutex_t * mut)
{
int i;
@@ -43,7 +43,7 @@ dump_mut (pthread_mutex_t * mut)
};
/* Helper, the opposite of pthread_cond_wait (cond, mut). */
-void
+static void
pthr_cond_signal_mutex (pthread_cond_t * cond, pthread_mutex_t * mut)
{
int err;
@@ -59,7 +59,7 @@ pthr_cond_signal_mutex (pthread_cond_t * cond, pthread_mutex_t * mut)
}
-void *
+static void *
thread_start (void *ptr)
{
struct thr_ctrl *tc = ptr;
diff --git a/linuxthreads/Examples/ex14.c b/linuxthreads/Examples/ex14.c
index 7788e521b8..406e03f346 100644
--- a/linuxthreads/Examples/ex14.c
+++ b/linuxthreads/Examples/ex14.c
@@ -92,7 +92,7 @@ worker (void *arg)
#define TEST_FUNCTION do_test ()
#define TIMEOUT 60
-int
+static int
do_test (void)
{
pthread_t threads[NTHREADS];
diff --git a/linuxthreads/Examples/ex2.c b/linuxthreads/Examples/ex2.c
index 70cb6b3986..f2556a4206 100644
--- a/linuxthreads/Examples/ex2.c
+++ b/linuxthreads/Examples/ex2.c
@@ -10,61 +10,66 @@
/* Circular buffer of integers. */
-struct prodcons {
- int buffer[BUFFER_SIZE]; /* the actual data */
- pthread_mutex_t lock; /* mutex ensuring exclusive access to buffer */
- int readpos, writepos; /* positions for reading and writing */
- pthread_cond_t notempty; /* signaled when buffer is not empty */
- pthread_cond_t notfull; /* signaled when buffer is not full */
+struct prodcons
+{
+ int buffer[BUFFER_SIZE]; /* the actual data */
+ pthread_mutex_t lock; /* mutex ensuring exclusive access to buffer */
+ int readpos, writepos; /* positions for reading and writing */
+ pthread_cond_t notempty; /* signaled when buffer is not empty */
+ pthread_cond_t notfull; /* signaled when buffer is not full */
};
/* Initialize a buffer */
-
-void init(struct prodcons * b)
+static void
+init (struct prodcons *b)
{
- pthread_mutex_init(&b->lock, NULL);
- pthread_cond_init(&b->notempty, NULL);
- pthread_cond_init(&b->notfull, NULL);
+ pthread_mutex_init (&b->lock, NULL);
+ pthread_cond_init (&b->notempty, NULL);
+ pthread_cond_init (&b->notfull, NULL);
b->readpos = 0;
b->writepos = 0;
}
/* Store an integer in the buffer */
-
-void put(struct prodcons * b, int data)
+static void
+put (struct prodcons *b, int data)
{
- pthread_mutex_lock(&b->lock);
+ pthread_mutex_lock (&b->lock);
/* Wait until buffer is not full */
- while ((b->writepos + 1) % BUFFER_SIZE == b->readpos) {
- pthread_cond_wait(&b->notfull, &b->lock);
- /* pthread_cond_wait reacquired b->lock before returning */
- }
+ while ((b->writepos + 1) % BUFFER_SIZE == b->readpos)
+ {
+ pthread_cond_wait (&b->notfull, &b->lock);
+ /* pthread_cond_wait reacquired b->lock before returning */
+ }
/* Write the data and advance write pointer */
b->buffer[b->writepos] = data;
b->writepos++;
- if (b->writepos >= BUFFER_SIZE) b->writepos = 0;
+ if (b->writepos >= BUFFER_SIZE)
+ b->writepos = 0;
/* Signal that the buffer is now not empty */
- pthread_cond_signal(&b->notempty);
- pthread_mutex_unlock(&b->lock);
+ pthread_cond_signal (&b->notempty);
+ pthread_mutex_unlock (&b->lock);
}
/* Read and remove an integer from the buffer */
-
-int get(struct prodcons * b)
+static int
+get (struct prodcons *b)
{
int data;
- pthread_mutex_lock(&b->lock);
+ pthread_mutex_lock (&b->lock);
/* Wait until buffer is not empty */
- while (b->writepos == b->readpos) {
- pthread_cond_wait(&b->notempty, &b->lock);
- }
+ while (b->writepos == b->readpos)
+ {
+ pthread_cond_wait (&b->notempty, &b->lock);
+ }
/* Read the data and advance read pointer */
data = b->buffer[b->readpos];
b->readpos++;
- if (b->readpos >= BUFFER_SIZE) b->readpos = 0;
+ if (b->readpos >= BUFFER_SIZE)
+ b->readpos = 0;
/* Signal that the buffer is now not full */
- pthread_cond_signal(&b->notfull);
- pthread_mutex_unlock(&b->lock);
+ pthread_cond_signal (&b->notfull);
+ pthread_mutex_unlock (&b->lock);
return data;
}
@@ -75,39 +80,45 @@ int get(struct prodcons * b)
struct prodcons buffer;
-void * producer(void * data)
+static void *
+producer (void *data)
{
int n;
- for (n = 0; n < 10000; n++) {
- printf("%d --->\n", n);
- put(&buffer, n);
- }
- put(&buffer, OVER);
+ for (n = 0; n < 10000; n++)
+ {
+ printf ("%d --->\n", n);
+ put (&buffer, n);
+ }
+ put (&buffer, OVER);
return NULL;
}
-void * consumer(void * data)
+static void *
+consumer (void *data)
{
int d;
- while (1) {
- d = get(&buffer);
- if (d == OVER) break;
- printf("---> %d\n", d);
- }
+ while (1)
+ {
+ d = get (&buffer);
+ if (d == OVER)
+ break;
+ printf ("---> %d\n", d);
+ }
return NULL;
}
-int main(void)
+int
+main (void)
{
pthread_t th_a, th_b;
- void * retval;
+ void *retval;
- init(&buffer);
+ init (&buffer);
/* Create the threads */
- pthread_create(&th_a, NULL, producer, 0);
- pthread_create(&th_b, NULL, consumer, 0);
+ pthread_create (&th_a, NULL, producer, 0);
+ pthread_create (&th_b, NULL, consumer, 0);
/* Wait until producer and consumer finish. */
- pthread_join(th_a, &retval);
- pthread_join(th_b, &retval);
+ pthread_join (th_a, &retval);
+ pthread_join (th_b, &retval);
return 0;
}
diff --git a/linuxthreads/Examples/ex4.c b/linuxthreads/Examples/ex4.c
index 86584de70d..5c8b929e22 100644
--- a/linuxthreads/Examples/ex4.c
+++ b/linuxthreads/Examples/ex4.c
@@ -14,10 +14,11 @@
#if 0
-char * str_accumulate(char * s)
+char *
+str_accumulate (char *s)
{
static char accu[1024] = { 0 };
- strcat(accu, s);
+ strcat (accu, s);
return accu;
}
@@ -35,73 +36,80 @@ static pthread_key_t str_key;
static pthread_once_t str_alloc_key_once = PTHREAD_ONCE_INIT;
/* Forward functions */
-static void str_alloc_key(void);
-static void str_alloc_destroy_accu(void * accu);
+static void str_alloc_key (void);
+static void str_alloc_destroy_accu (void *accu);
/* Thread-safe version of str_accumulate */
-char * str_accumulate(const char * s)
+static char *
+str_accumulate (const char *s)
{
- char * accu;
+ char *accu;
/* Make sure the key is allocated */
- pthread_once(&str_alloc_key_once, str_alloc_key);
+ pthread_once (&str_alloc_key_once, str_alloc_key);
/* Get the thread-specific data associated with the key */
- accu = (char *) pthread_getspecific(str_key);
+ accu = (char *) pthread_getspecific (str_key);
/* It's initially NULL, meaning that we must allocate the buffer first. */
- if (accu == NULL) {
- accu = malloc(1024);
- if (accu == NULL) return NULL;
- accu[0] = 0;
- /* Store the buffer pointer in the thread-specific data. */
- pthread_setspecific(str_key, (void *) accu);
- printf("Thread %lx: allocating buffer at %p\n", pthread_self(), accu);
- }
+ if (accu == NULL)
+ {
+ accu = malloc (1024);
+ if (accu == NULL)
+ return NULL;
+ accu[0] = 0;
+ /* Store the buffer pointer in the thread-specific data. */
+ pthread_setspecific (str_key, (void *) accu);
+ printf ("Thread %lx: allocating buffer at %p\n", pthread_self (), accu);
+ }
/* Now we can use accu just as in the non thread-safe code. */
- strcat(accu, s);
+ strcat (accu, s);
return accu;
}
/* Function to allocate the key for str_alloc thread-specific data. */
-static void str_alloc_key(void)
+static void
+str_alloc_key (void)
{
- pthread_key_create(&str_key, str_alloc_destroy_accu);
- printf("Thread %lx: allocated key %d\n", pthread_self(), str_key);
+ pthread_key_create (&str_key, str_alloc_destroy_accu);
+ printf ("Thread %lx: allocated key %d\n", pthread_self (), str_key);
}
/* Function to free the buffer when the thread exits. */
/* Called only when the thread-specific data is not NULL. */
-static void str_alloc_destroy_accu(void * accu)
+static void
+str_alloc_destroy_accu (void *accu)
{
- printf("Thread %lx: freeing buffer at %p\n", pthread_self(), accu);
- free(accu);
+ printf ("Thread %lx: freeing buffer at %p\n", pthread_self (), accu);
+ free (accu);
}
/* Test program */
-void * process(void * arg)
+static void *
+process (void *arg)
{
- char * res;
- res = str_accumulate("Result of ");
- res = str_accumulate((char *) arg);
- res = str_accumulate(" thread");
- printf("Thread %lx: \"%s\"\n", pthread_self(), res);
+ char *res;
+ res = str_accumulate ("Result of ");
+ res = str_accumulate ((char *) arg);
+ res = str_accumulate (" thread");
+ printf ("Thread %lx: \"%s\"\n", pthread_self (), res);
return NULL;
}
-int main(int argc, char ** argv)
+int
+main (int argc, char **argv)
{
- char * res;
+ char *res;
pthread_t th1, th2;
- res = str_accumulate("Result of ");
- pthread_create(&th1, NULL, process, (void *) "first");
- pthread_create(&th2, NULL, process, (void *) "second");
- res = str_accumulate("initial thread");
- printf("Thread %lx: \"%s\"\n", pthread_self(), res);
- pthread_join(th1, NULL);
- pthread_join(th2, NULL);
+ res = str_accumulate ("Result of ");
+ pthread_create (&th1, NULL, process, (void *) "first");
+ pthread_create (&th2, NULL, process, (void *) "second");
+ res = str_accumulate ("initial thread");
+ printf ("Thread %lx: \"%s\"\n", pthread_self (), res);
+ pthread_join (th1, NULL);
+ pthread_join (th2, NULL);
return 0;
}
diff --git a/linuxthreads/Examples/ex5.c b/linuxthreads/Examples/ex5.c
index 475de0e0c5..d39d487603 100644
--- a/linuxthreads/Examples/ex5.c
+++ b/linuxthreads/Examples/ex5.c
@@ -10,50 +10,56 @@
/* Circular buffer of integers. */
-struct prodcons {
- int buffer[BUFFER_SIZE]; /* the actual data */
- int readpos, writepos; /* positions for reading and writing */
- sem_t sem_read; /* number of elements available for reading */
- sem_t sem_write; /* number of locations available for writing */
+struct prodcons
+{
+ int buffer[BUFFER_SIZE]; /* the actual data */
+ int readpos, writepos; /* positions for reading and writing */
+ sem_t sem_read; /* number of elements available for reading */
+ sem_t sem_write; /* number of locations available for writing */
};
/* Initialize a buffer */
-void init(struct prodcons * b)
+static void
+init (struct prodcons *b)
{
- sem_init(&b->sem_write, 0, BUFFER_SIZE - 1);
- sem_init(&b->sem_read, 0, 0);
+ sem_init (&b->sem_write, 0, BUFFER_SIZE - 1);
+ sem_init (&b->sem_read, 0, 0);
b->readpos = 0;
b->writepos = 0;
}
/* Store an integer in the buffer */
-void put(struct prodcons * b, int data)
+static void
+put (struct prodcons *b, int data)
{
/* Wait until buffer is not full */
- sem_wait(&b->sem_write);
+ sem_wait (&b->sem_write);
/* Write the data and advance write pointer */
b->buffer[b->writepos] = data;
b->writepos++;
- if (b->writepos >= BUFFER_SIZE) b->writepos = 0;
+ if (b->writepos >= BUFFER_SIZE)
+ b->writepos = 0;
/* Signal that the buffer contains one more element for reading */
- sem_post(&b->sem_read);
+ sem_post (&b->sem_read);
}
/* Read and remove an integer from the buffer */
-int get(struct prodcons * b)
+static int
+get (struct prodcons *b)
{
int data;
/* Wait until buffer is not empty */
- sem_wait(&b->sem_read);
+ sem_wait (&b->sem_read);
/* Read the data and advance read pointer */
data = b->buffer[b->readpos];
b->readpos++;
- if (b->readpos >= BUFFER_SIZE) b->readpos = 0;
+ if (b->readpos >= BUFFER_SIZE)
+ b->readpos = 0;
/* Signal that the buffer has now one more location for writing */
- sem_post(&b->sem_write);
+ sem_post (&b->sem_write);
return data;
}
@@ -64,39 +70,45 @@ int get(struct prodcons * b)
struct prodcons buffer;
-void * producer(void * data)
+static void *
+producer (void *data)
{
int n;
- for (n = 0; n < 10000; n++) {
- printf("%d --->\n", n);
- put(&buffer, n);
- }
- put(&buffer, OVER);
+ for (n = 0; n < 10000; n++)
+ {
+ printf ("%d --->\n", n);
+ put (&buffer, n);
+ }
+ put (&buffer, OVER);
return NULL;
}
-void * consumer(void * data)
+static void *
+consumer (void *data)
{
int d;
- while (1) {
- d = get(&buffer);
- if (d == OVER) break;
- printf("---> %d\n", d);
- }
+ while (1)
+ {
+ d = get (&buffer);
+ if (d == OVER)
+ break;
+ printf ("---> %d\n", d);
+ }
return NULL;
}
-int main(void)
+int
+main (void)
{
pthread_t th_a, th_b;
- void * retval;
+ void *retval;
- init(&buffer);
+ init (&buffer);
/* Create the threads */
- pthread_create(&th_a, NULL, producer, 0);
- pthread_create(&th_b, NULL, consumer, 0);
+ pthread_create (&th_a, NULL, producer, 0);
+ pthread_create (&th_b, NULL, consumer, 0);
/* Wait until producer and consumer finish. */
- pthread_join(th_a, &retval);
- pthread_join(th_b, &retval);
+ pthread_join (th_a, &retval);
+ pthread_join (th_b, &retval);
return 0;
}
diff --git a/linuxthreads/Examples/ex7.c b/linuxthreads/Examples/ex7.c
index 94b708b56b..d9db33c5cd 100644
--- a/linuxthreads/Examples/ex7.c
+++ b/linuxthreads/Examples/ex7.c
@@ -9,32 +9,37 @@
#include <stdlib.h>
#include <pthread.h>
-#define NTHREADS 20 /* number of threads */
+#define NTHREADS 20 /* number of threads */
-static void *thread(void *arg)
+static void *
+thread (void *arg)
{
- printf("thread terminating\n");
+ printf ("thread terminating\n");
return 0;
}
-void cleanup(void)
+static void
+cleanup (void)
{
- printf("atexit handler called\n");
+ printf ("atexit handler called\n");
}
-int main(void)
+int
+main (void)
{
int i;
- atexit(cleanup);
+ atexit (cleanup);
- for (i = 0; i < NTHREADS; i++) {
- pthread_t id;
- if (pthread_create(&id, 0, thread, 0) != 0) {
- fprintf(stderr, "pthread_create failed\n");
- abort();
+ for (i = 0; i < NTHREADS; i++)
+ {
+ pthread_t id;
+ if (pthread_create (&id, 0, thread, 0) != 0)
+ {
+ fprintf (stderr, "pthread_create failed\n");
+ abort ();
+ }
}
- }
- pthread_exit(0);
+ pthread_exit (0);
}