1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-08-07 06:43:00 +03:00

* 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 ----------------------------------------------------------------------
This commit is contained in:
Andreas Jaeger
2000-12-27 17:14:56 +00:00
parent 2111285729
commit a375a533a2
8 changed files with 199 additions and 156 deletions

View File

@@ -7,29 +7,36 @@
#include <unistd.h> #include <unistd.h>
#include "pthread.h" #include "pthread.h"
void * process(void * arg) static void *
process (void *arg)
{ {
int i; int i;
fprintf(stderr, "Starting process %s\n", (char *) arg); fprintf (stderr, "Starting process %s\n", (char *) arg);
for (i = 0; i < 10000; i++) { for (i = 0; i < 10000; i++)
write(1, (char *) arg, 1); {
write (1, (char *) arg, 1);
} }
return NULL; return NULL;
} }
int main(void) int
main (void)
{ {
int retcode; int retcode;
pthread_t th_a, th_b; pthread_t th_a, th_b;
void * retval; void *retval;
retcode = pthread_create(&th_a, NULL, process, (void *) "a"); retcode = pthread_create (&th_a, NULL, process, (void *) "a");
if (retcode != 0) fprintf(stderr, "create a failed %d\n", retcode); if (retcode != 0)
retcode = pthread_create(&th_b, NULL, process, (void *) "b"); fprintf (stderr, "create a failed %d\n", retcode);
if (retcode != 0) fprintf(stderr, "create b failed %d\n", retcode); retcode = pthread_create (&th_b, NULL, process, (void *) "b");
retcode = pthread_join(th_a, &retval); if (retcode != 0)
if (retcode != 0) fprintf(stderr, "join a failed %d\n", retcode); fprintf (stderr, "create b failed %d\n", retcode);
retcode = pthread_join(th_b, &retval); retcode = pthread_join (th_a, &retval);
if (retcode != 0) fprintf(stderr, "join b failed %d\n", retcode); 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; return 0;
} }

View File

@@ -32,7 +32,7 @@ struct thr_ctrl
int retval; int retval;
}; };
void static void
dump_mut (pthread_mutex_t * mut) dump_mut (pthread_mutex_t * mut)
{ {
int i; int i;
@@ -43,7 +43,7 @@ dump_mut (pthread_mutex_t * mut)
}; };
/* Helper, the opposite of pthread_cond_wait (cond, 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) pthr_cond_signal_mutex (pthread_cond_t * cond, pthread_mutex_t * mut)
{ {
int err; 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) thread_start (void *ptr)
{ {
struct thr_ctrl *tc = ptr; struct thr_ctrl *tc = ptr;

View File

@@ -92,7 +92,7 @@ worker (void *arg)
#define TEST_FUNCTION do_test () #define TEST_FUNCTION do_test ()
#define TIMEOUT 60 #define TIMEOUT 60
int static int
do_test (void) do_test (void)
{ {
pthread_t threads[NTHREADS]; pthread_t threads[NTHREADS];

View File

@@ -10,7 +10,8 @@
/* Circular buffer of integers. */ /* Circular buffer of integers. */
struct prodcons { struct prodcons
{
int buffer[BUFFER_SIZE]; /* the actual data */ int buffer[BUFFER_SIZE]; /* the actual data */
pthread_mutex_t lock; /* mutex ensuring exclusive access to buffer */ pthread_mutex_t lock; /* mutex ensuring exclusive access to buffer */
int readpos, writepos; /* positions for reading and writing */ int readpos, writepos; /* positions for reading and writing */
@@ -19,52 +20,56 @@ struct prodcons {
}; };
/* Initialize a buffer */ /* Initialize a buffer */
static void
void init(struct prodcons * b) init (struct prodcons *b)
{ {
pthread_mutex_init(&b->lock, NULL); pthread_mutex_init (&b->lock, NULL);
pthread_cond_init(&b->notempty, NULL); pthread_cond_init (&b->notempty, NULL);
pthread_cond_init(&b->notfull, NULL); pthread_cond_init (&b->notfull, NULL);
b->readpos = 0; b->readpos = 0;
b->writepos = 0; b->writepos = 0;
} }
/* Store an integer in the buffer */ /* Store an integer in the buffer */
static void
void put(struct prodcons * b, int data) put (struct prodcons *b, int data)
{ {
pthread_mutex_lock(&b->lock); pthread_mutex_lock (&b->lock);
/* Wait until buffer is not full */ /* Wait until buffer is not full */
while ((b->writepos + 1) % BUFFER_SIZE == b->readpos) { while ((b->writepos + 1) % BUFFER_SIZE == b->readpos)
pthread_cond_wait(&b->notfull, &b->lock); {
pthread_cond_wait (&b->notfull, &b->lock);
/* pthread_cond_wait reacquired b->lock before returning */ /* pthread_cond_wait reacquired b->lock before returning */
} }
/* Write the data and advance write pointer */ /* Write the data and advance write pointer */
b->buffer[b->writepos] = data; b->buffer[b->writepos] = data;
b->writepos++; 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 */ /* Signal that the buffer is now not empty */
pthread_cond_signal(&b->notempty); pthread_cond_signal (&b->notempty);
pthread_mutex_unlock(&b->lock); pthread_mutex_unlock (&b->lock);
} }
/* Read and remove an integer from the buffer */ /* Read and remove an integer from the buffer */
static int
int get(struct prodcons * b) get (struct prodcons *b)
{ {
int data; int data;
pthread_mutex_lock(&b->lock); pthread_mutex_lock (&b->lock);
/* Wait until buffer is not empty */ /* Wait until buffer is not empty */
while (b->writepos == b->readpos) { while (b->writepos == b->readpos)
pthread_cond_wait(&b->notempty, &b->lock); {
pthread_cond_wait (&b->notempty, &b->lock);
} }
/* Read the data and advance read pointer */ /* Read the data and advance read pointer */
data = b->buffer[b->readpos]; data = b->buffer[b->readpos];
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 */ /* Signal that the buffer is now not full */
pthread_cond_signal(&b->notfull); pthread_cond_signal (&b->notfull);
pthread_mutex_unlock(&b->lock); pthread_mutex_unlock (&b->lock);
return data; return data;
} }
@@ -75,39 +80,45 @@ int get(struct prodcons * b)
struct prodcons buffer; struct prodcons buffer;
void * producer(void * data) static void *
producer (void *data)
{ {
int n; int n;
for (n = 0; n < 10000; n++) { for (n = 0; n < 10000; n++)
printf("%d --->\n", n); {
put(&buffer, n); printf ("%d --->\n", n);
put (&buffer, n);
} }
put(&buffer, OVER); put (&buffer, OVER);
return NULL; return NULL;
} }
void * consumer(void * data) static void *
consumer (void *data)
{ {
int d; int d;
while (1) { while (1)
d = get(&buffer); {
if (d == OVER) break; d = get (&buffer);
printf("---> %d\n", d); if (d == OVER)
break;
printf ("---> %d\n", d);
} }
return NULL; return NULL;
} }
int main(void) int
main (void)
{ {
pthread_t th_a, th_b; pthread_t th_a, th_b;
void * retval; void *retval;
init(&buffer); init (&buffer);
/* Create the threads */ /* Create the threads */
pthread_create(&th_a, NULL, producer, 0); pthread_create (&th_a, NULL, producer, 0);
pthread_create(&th_b, NULL, consumer, 0); pthread_create (&th_b, NULL, consumer, 0);
/* Wait until producer and consumer finish. */ /* Wait until producer and consumer finish. */
pthread_join(th_a, &retval); pthread_join (th_a, &retval);
pthread_join(th_b, &retval); pthread_join (th_b, &retval);
return 0; return 0;
} }

View File

@@ -14,10 +14,11 @@
#if 0 #if 0
char * str_accumulate(char * s) char *
str_accumulate (char *s)
{ {
static char accu[1024] = { 0 }; static char accu[1024] = { 0 };
strcat(accu, s); strcat (accu, s);
return accu; return accu;
} }
@@ -35,73 +36,80 @@ static pthread_key_t str_key;
static pthread_once_t str_alloc_key_once = PTHREAD_ONCE_INIT; static pthread_once_t str_alloc_key_once = PTHREAD_ONCE_INIT;
/* Forward functions */ /* Forward functions */
static void str_alloc_key(void); static void str_alloc_key (void);
static void str_alloc_destroy_accu(void * accu); static void str_alloc_destroy_accu (void *accu);
/* Thread-safe version of str_accumulate */ /* 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 */ /* 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 */ /* 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. */ /* It's initially NULL, meaning that we must allocate the buffer first. */
if (accu == NULL) { if (accu == NULL)
accu = malloc(1024); {
if (accu == NULL) return NULL; accu = malloc (1024);
if (accu == NULL)
return NULL;
accu[0] = 0; accu[0] = 0;
/* Store the buffer pointer in the thread-specific data. */ /* Store the buffer pointer in the thread-specific data. */
pthread_setspecific(str_key, (void *) accu); pthread_setspecific (str_key, (void *) accu);
printf("Thread %lx: allocating buffer at %p\n", pthread_self(), 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. */ /* Now we can use accu just as in the non thread-safe code. */
strcat(accu, s); strcat (accu, s);
return accu; return accu;
} }
/* Function to allocate the key for str_alloc thread-specific data. */ /* 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); pthread_key_create (&str_key, str_alloc_destroy_accu);
printf("Thread %lx: allocated key %d\n", pthread_self(), str_key); printf ("Thread %lx: allocated key %d\n", pthread_self (), str_key);
} }
/* Function to free the buffer when the thread exits. */ /* Function to free the buffer when the thread exits. */
/* Called only when the thread-specific data is not NULL. */ /* 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); printf ("Thread %lx: freeing buffer at %p\n", pthread_self (), accu);
free(accu); free (accu);
} }
/* Test program */ /* Test program */
void * process(void * arg) static void *
process (void *arg)
{ {
char * res; char *res;
res = str_accumulate("Result of "); res = str_accumulate ("Result of ");
res = str_accumulate((char *) arg); res = str_accumulate ((char *) arg);
res = str_accumulate(" thread"); res = str_accumulate (" thread");
printf("Thread %lx: \"%s\"\n", pthread_self(), res); printf ("Thread %lx: \"%s\"\n", pthread_self (), res);
return NULL; return NULL;
} }
int main(int argc, char ** argv) int
main (int argc, char **argv)
{ {
char * res; char *res;
pthread_t th1, th2; pthread_t th1, th2;
res = str_accumulate("Result of "); res = str_accumulate ("Result of ");
pthread_create(&th1, NULL, process, (void *) "first"); pthread_create (&th1, NULL, process, (void *) "first");
pthread_create(&th2, NULL, process, (void *) "second"); pthread_create (&th2, NULL, process, (void *) "second");
res = str_accumulate("initial thread"); res = str_accumulate ("initial thread");
printf("Thread %lx: \"%s\"\n", pthread_self(), res); printf ("Thread %lx: \"%s\"\n", pthread_self (), res);
pthread_join(th1, NULL); pthread_join (th1, NULL);
pthread_join(th2, NULL); pthread_join (th2, NULL);
return 0; return 0;
} }

View File

@@ -10,7 +10,8 @@
/* Circular buffer of integers. */ /* Circular buffer of integers. */
struct prodcons { struct prodcons
{
int buffer[BUFFER_SIZE]; /* the actual data */ int buffer[BUFFER_SIZE]; /* the actual data */
int readpos, writepos; /* positions for reading and writing */ int readpos, writepos; /* positions for reading and writing */
sem_t sem_read; /* number of elements available for reading */ sem_t sem_read; /* number of elements available for reading */
@@ -19,41 +20,46 @@ struct prodcons {
/* Initialize a buffer */ /* 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_write, 0, BUFFER_SIZE - 1);
sem_init(&b->sem_read, 0, 0); sem_init (&b->sem_read, 0, 0);
b->readpos = 0; b->readpos = 0;
b->writepos = 0; b->writepos = 0;
} }
/* Store an integer in the buffer */ /* 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 */ /* Wait until buffer is not full */
sem_wait(&b->sem_write); sem_wait (&b->sem_write);
/* Write the data and advance write pointer */ /* Write the data and advance write pointer */
b->buffer[b->writepos] = data; b->buffer[b->writepos] = data;
b->writepos++; 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 */ /* 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 */ /* Read and remove an integer from the buffer */
int get(struct prodcons * b) static int
get (struct prodcons *b)
{ {
int data; int data;
/* Wait until buffer is not empty */ /* Wait until buffer is not empty */
sem_wait(&b->sem_read); sem_wait (&b->sem_read);
/* Read the data and advance read pointer */ /* Read the data and advance read pointer */
data = b->buffer[b->readpos]; data = b->buffer[b->readpos];
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 */ /* Signal that the buffer has now one more location for writing */
sem_post(&b->sem_write); sem_post (&b->sem_write);
return data; return data;
} }
@@ -64,39 +70,45 @@ int get(struct prodcons * b)
struct prodcons buffer; struct prodcons buffer;
void * producer(void * data) static void *
producer (void *data)
{ {
int n; int n;
for (n = 0; n < 10000; n++) { for (n = 0; n < 10000; n++)
printf("%d --->\n", n); {
put(&buffer, n); printf ("%d --->\n", n);
put (&buffer, n);
} }
put(&buffer, OVER); put (&buffer, OVER);
return NULL; return NULL;
} }
void * consumer(void * data) static void *
consumer (void *data)
{ {
int d; int d;
while (1) { while (1)
d = get(&buffer); {
if (d == OVER) break; d = get (&buffer);
printf("---> %d\n", d); if (d == OVER)
break;
printf ("---> %d\n", d);
} }
return NULL; return NULL;
} }
int main(void) int
main (void)
{ {
pthread_t th_a, th_b; pthread_t th_a, th_b;
void * retval; void *retval;
init(&buffer); init (&buffer);
/* Create the threads */ /* Create the threads */
pthread_create(&th_a, NULL, producer, 0); pthread_create (&th_a, NULL, producer, 0);
pthread_create(&th_b, NULL, consumer, 0); pthread_create (&th_b, NULL, consumer, 0);
/* Wait until producer and consumer finish. */ /* Wait until producer and consumer finish. */
pthread_join(th_a, &retval); pthread_join (th_a, &retval);
pthread_join(th_b, &retval); pthread_join (th_b, &retval);
return 0; return 0;
} }

View File

@@ -11,30 +11,35 @@
#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; 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; int i;
atexit(cleanup); atexit (cleanup);
for (i = 0; i < NTHREADS; i++) { for (i = 0; i < NTHREADS; i++)
{
pthread_t id; pthread_t id;
if (pthread_create(&id, 0, thread, 0) != 0) { if (pthread_create (&id, 0, thread, 0) != 0)
fprintf(stderr, "pthread_create failed\n"); {
abort(); fprintf (stderr, "pthread_create failed\n");
abort ();
} }
} }
pthread_exit(0); pthread_exit (0);
} }

View File

@@ -130,7 +130,7 @@ worker (void *arg)
#define TEST_FUNCTION do_test () #define TEST_FUNCTION do_test ()
int static int
do_test (void) do_test (void)
{ {
pthread_t threads[NTHREADS]; pthread_t threads[NTHREADS];