mirror of
https://sourceware.org/git/glibc.git
synced 2025-07-30 22:43:12 +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:
@ -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++) {
|
||||
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;
|
||||
|
||||
retcode = pthread_create (&th_a, NULL, process, (void *) "a");
|
||||
if (retcode != 0) fprintf(stderr, "create a failed %d\n", retcode);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
if (retcode != 0)
|
||||
fprintf (stderr, "join b failed %d\n", retcode);
|
||||
return 0;
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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];
|
||||
|
@ -10,7 +10,8 @@
|
||||
|
||||
/* Circular buffer of integers. */
|
||||
|
||||
struct prodcons {
|
||||
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 */
|
||||
@ -19,8 +20,8 @@ struct prodcons {
|
||||
};
|
||||
|
||||
/* 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);
|
||||
@ -30,38 +31,42 @@ void init(struct prodcons * b)
|
||||
}
|
||||
|
||||
/* 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);
|
||||
/* 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 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);
|
||||
}
|
||||
|
||||
/* 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);
|
||||
/* Wait until buffer is not empty */
|
||||
while (b->writepos == b->readpos) {
|
||||
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);
|
||||
@ -75,10 +80,12 @@ 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++) {
|
||||
for (n = 0; n < 10000; n++)
|
||||
{
|
||||
printf ("%d --->\n", n);
|
||||
put (&buffer, n);
|
||||
}
|
||||
@ -86,18 +93,22 @@ void * producer(void * data)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void * consumer(void * data)
|
||||
static void *
|
||||
consumer (void *data)
|
||||
{
|
||||
int d;
|
||||
while (1) {
|
||||
while (1)
|
||||
{
|
||||
d = get (&buffer);
|
||||
if (d == OVER) break;
|
||||
if (d == OVER)
|
||||
break;
|
||||
printf ("---> %d\n", d);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
int
|
||||
main (void)
|
||||
{
|
||||
pthread_t th_a, th_b;
|
||||
void *retval;
|
||||
|
@ -14,7 +14,8 @@
|
||||
|
||||
#if 0
|
||||
|
||||
char * str_accumulate(char * s)
|
||||
char *
|
||||
str_accumulate (char *s)
|
||||
{
|
||||
static char accu[1024] = { 0 };
|
||||
strcat (accu, s);
|
||||
@ -40,7 +41,8 @@ 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;
|
||||
|
||||
@ -49,9 +51,11 @@ char * str_accumulate(const char * s)
|
||||
/* Get the thread-specific data associated with the key */
|
||||
accu = (char *) pthread_getspecific (str_key);
|
||||
/* 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;
|
||||
if (accu == NULL)
|
||||
return NULL;
|
||||
accu[0] = 0;
|
||||
/* Store the buffer pointer in the thread-specific data. */
|
||||
pthread_setspecific (str_key, (void *) accu);
|
||||
@ -64,7 +68,8 @@ char * str_accumulate(const char * s)
|
||||
|
||||
/* 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);
|
||||
@ -73,7 +78,8 @@ static void str_alloc_key(void)
|
||||
/* 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);
|
||||
@ -81,7 +87,8 @@ static void str_alloc_destroy_accu(void * accu)
|
||||
|
||||
/* Test program */
|
||||
|
||||
void * process(void * arg)
|
||||
static void *
|
||||
process (void *arg)
|
||||
{
|
||||
char *res;
|
||||
res = str_accumulate ("Result of ");
|
||||
@ -91,7 +98,8 @@ void * process(void * arg)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
char *res;
|
||||
pthread_t th1, th2;
|
||||
|
@ -10,7 +10,8 @@
|
||||
|
||||
/* Circular buffer of integers. */
|
||||
|
||||
struct prodcons {
|
||||
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 */
|
||||
@ -19,7 +20,8 @@ struct prodcons {
|
||||
|
||||
/* 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);
|
||||
@ -29,21 +31,24 @@ void init(struct prodcons * b)
|
||||
|
||||
/* 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);
|
||||
/* 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);
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
@ -51,7 +56,8 @@ int get(struct prodcons * b)
|
||||
/* 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);
|
||||
return data;
|
||||
@ -64,10 +70,12 @@ 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++) {
|
||||
for (n = 0; n < 10000; n++)
|
||||
{
|
||||
printf ("%d --->\n", n);
|
||||
put (&buffer, n);
|
||||
}
|
||||
@ -75,18 +83,22 @@ void * producer(void * data)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void * consumer(void * data)
|
||||
static void *
|
||||
consumer (void *data)
|
||||
{
|
||||
int d;
|
||||
while (1) {
|
||||
while (1)
|
||||
{
|
||||
d = get (&buffer);
|
||||
if (d == OVER) break;
|
||||
if (d == OVER)
|
||||
break;
|
||||
printf ("---> %d\n", d);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
int
|
||||
main (void)
|
||||
{
|
||||
pthread_t th_a, th_b;
|
||||
void *retval;
|
||||
|
@ -11,26 +11,31 @@
|
||||
|
||||
#define NTHREADS 20 /* number of threads */
|
||||
|
||||
static void *thread(void *arg)
|
||||
static void *
|
||||
thread (void *arg)
|
||||
{
|
||||
printf ("thread terminating\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void cleanup(void)
|
||||
static void
|
||||
cleanup (void)
|
||||
{
|
||||
printf ("atexit handler called\n");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
int
|
||||
main (void)
|
||||
{
|
||||
int i;
|
||||
|
||||
atexit (cleanup);
|
||||
|
||||
for (i = 0; i < NTHREADS; i++) {
|
||||
for (i = 0; i < NTHREADS; i++)
|
||||
{
|
||||
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 ();
|
||||
}
|
||||
|
@ -130,7 +130,7 @@ worker (void *arg)
|
||||
|
||||
|
||||
#define TEST_FUNCTION do_test ()
|
||||
int
|
||||
static int
|
||||
do_test (void)
|
||||
{
|
||||
pthread_t threads[NTHREADS];
|
||||
|
Reference in New Issue
Block a user