#include <stdlib.h>
#include <pthread.h>
struct foo{
int count;
struct foo *
foo_alloc(void)
{
struct foo *fp;
if((fp=malloc(sizeof(struct foo)))!=NULL);
{
fp->count = 1;
if(pthread_mutex_init(&fp->mutex,NULL)!=0)
{
free(fp);
return(NULL);
return (fp);
}
void
mutex_hold(struct foo *fp)
{
pthread_mutex_lock(&fp->mutex);
fp->count++;
pthread_mutex_unlock(&fp->mutex);
}
void
mutex_rele(struct foo *fp)
{
pthread_mutex_lock(&fp->mutex);
if(--fp->count==0){
pthread_mutex_destroy(&fp->mutex);
free(fp);
}else {
pthread_mutex_unlock(&fp->mutex);
}
}
#include <pthread.h>
struct foo{
int count;
pthread_mutex_t mutex;
/*other struction*/
};struct foo *
foo_alloc(void)
{
struct foo *fp;
if((fp=malloc(sizeof(struct foo)))!=NULL);
{
fp->count = 1;
if(pthread_mutex_init(&fp->mutex,NULL)!=0)
{
free(fp);
return(NULL);
}
/*initilize other parts*/
}return (fp);
}
void
mutex_hold(struct foo *fp)
{
pthread_mutex_lock(&fp->mutex);
fp->count++;
pthread_mutex_unlock(&fp->mutex);
}
void
mutex_rele(struct foo *fp)
{
pthread_mutex_lock(&fp->mutex);
if(--fp->count==0){
pthread_mutex_destroy(&fp->mutex);
free(fp);
}else {
pthread_mutex_unlock(&fp->mutex);
}
}