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:
@@ -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)
|
||||||
|
fprintf (stderr, "create a failed %d\n", retcode);
|
||||||
retcode = pthread_create (&th_b, NULL, process, (void *) "b");
|
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);
|
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);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@@ -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;
|
||||||
|
@@ -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];
|
||||||
|
@@ -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,8 +20,8 @@ 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);
|
||||||
@@ -30,38 +31,42 @@ void init(struct prodcons * b)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* 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);
|
||||||
@@ -75,10 +80,12 @@ 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);
|
printf ("%d --->\n", n);
|
||||||
put (&buffer, n);
|
put (&buffer, n);
|
||||||
}
|
}
|
||||||
@@ -86,18 +93,22 @@ void * producer(void * data)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void * consumer(void * data)
|
static void *
|
||||||
|
consumer (void *data)
|
||||||
{
|
{
|
||||||
int d;
|
int d;
|
||||||
while (1) {
|
while (1)
|
||||||
|
{
|
||||||
d = get (&buffer);
|
d = get (&buffer);
|
||||||
if (d == OVER) break;
|
if (d == OVER)
|
||||||
|
break;
|
||||||
printf ("---> %d\n", d);
|
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;
|
||||||
|
@@ -14,7 +14,8 @@
|
|||||||
|
|
||||||
#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);
|
||||||
@@ -40,7 +41,8 @@ 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;
|
||||||
|
|
||||||
@@ -49,9 +51,11 @@ char * str_accumulate(const char * s)
|
|||||||
/* 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);
|
accu = malloc (1024);
|
||||||
if (accu == NULL) return NULL;
|
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);
|
||||||
@@ -64,7 +68,8 @@ char * str_accumulate(const char * s)
|
|||||||
|
|
||||||
/* 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);
|
||||||
@@ -73,7 +78,8 @@ static void str_alloc_key(void)
|
|||||||
/* 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);
|
||||||
@@ -81,7 +87,8 @@ static void str_alloc_destroy_accu(void * 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 ");
|
||||||
@@ -91,7 +98,8 @@ void * process(void * arg)
|
|||||||
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;
|
||||||
|
@@ -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,7 +20,8 @@ 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);
|
||||||
@@ -29,21 +31,24 @@ void init(struct prodcons * b)
|
|||||||
|
|
||||||
/* 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 */
|
||||||
@@ -51,7 +56,8 @@ int get(struct prodcons * b)
|
|||||||
/* 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,10 +70,12 @@ 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);
|
printf ("%d --->\n", n);
|
||||||
put (&buffer, n);
|
put (&buffer, n);
|
||||||
}
|
}
|
||||||
@@ -75,18 +83,22 @@ void * producer(void * data)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void * consumer(void * data)
|
static void *
|
||||||
|
consumer (void *data)
|
||||||
{
|
{
|
||||||
int d;
|
int d;
|
||||||
while (1) {
|
while (1)
|
||||||
|
{
|
||||||
d = get (&buffer);
|
d = get (&buffer);
|
||||||
if (d == OVER) break;
|
if (d == OVER)
|
||||||
|
break;
|
||||||
printf ("---> %d\n", d);
|
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;
|
||||||
|
@@ -11,26 +11,31 @@
|
|||||||
|
|
||||||
#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");
|
fprintf (stderr, "pthread_create failed\n");
|
||||||
abort ();
|
abort ();
|
||||||
}
|
}
|
||||||
|
@@ -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];
|
||||||
|
Reference in New Issue
Block a user