diff --git a/src/lock/hash_lock.c b/src/lock/hash_lock.c index 67113ac..a5bbb4b 100644 --- a/src/lock/hash_lock.c +++ b/src/lock/hash_lock.c @@ -3,18 +3,87 @@ #include #include -void hashInit(hash_lock_t* bucket) { - perror("This function is not implemented"); +void hashInit(hash_lock_t* bucket) +{ + for(int i = 0;itable[i].head = NULL; + pthread_mutex_init(&bucket->table[i].mutex,NULL); + } } +int getValue(hash_lock_t* bucket, int key) +{ + int which = HASH(key); + pthread_mutex_lock(&bucket->table[which].mutex); + Hlist p = bucket->table[which].head; //.h中规定的节点指针,指向链表的头节点 + while(NULL != p) + { + if(p->key == key) + { + pthread_mutex_unlock(&bucket->table[which].mutex); + return p->value; + } + p = p->next; + } + pthread_mutex_unlock(&bucket->table[which].mutex); + return -1; -int getValue(hash_lock_t* bucket, int key) { - perror("This function is not implemented"); } -void insert(hash_lock_t* bucket, int key, int value) { - perror("This function is not implemented"); +void insert(hash_lock_t* bucket, int key, int value) +{ + int which = HASH(key); + pthread_mutex_lock(&bucket->table[which].mutex); + Hlist p = bucket->table[which].head; + while(NULL != p) + { + if(p->key == key) + { + p->value = value; + pthread_mutex_unlock(&bucket->table[which].mutex); + return; + } + p=p->next; + } + Hlist NewNode = (Hlist)malloc(sizeof(Hnode)); + NewNode->key = key; + NewNode->value = value; + NewNode->next = bucket->table[which].head; + bucket->table[which].head = NewNode; + pthread_mutex_unlock(&bucket->table[which].mutex); + return; + } -int setKey(hash_lock_t* bucket, int key, int new_key) { - perror("This function is not implemented"); -} \ No newline at end of file +int setKey(hash_lock_t* bucket, int key, int new_key) +{ + int old_which = HASH(key); + int new_which = HASH(new_key); + pthread_mutex_lock(&bucket->table[old_which].mutex); + Hlist p = bucket->table[old_which].head; + Hlist former = NULL;//记录前面一个 + while(NULL!=p) + { + if(p->key == key) + { + if(former == NULL) + { + bucket->table[old_which].head = p->next; + } + else + { + former->next = p->next; + } + p->key = new_key; + p->next = bucket->table[new_which].head; + bucket->table[new_which].head = p; + + pthread_mutex_unlock(&bucket->table[old_which].mutex); + return 0; + } + former = p; + p = p->next; + } + pthread_mutex_unlock(&bucket->table[old_which].mutex); + return -1; +} diff --git a/src/lock/list_lock.c b/src/lock/list_lock.c index 51b13c3..b97fd8f 100644 --- a/src/lock/list_lock.c +++ b/src/lock/list_lock.c @@ -3,18 +3,49 @@ #include #include -void listInit(list_lock_t* list) { - perror("This function is not implemented"); +void listInit(list_lock_t* list) +{ + list->head = NULL; + pthread_mutex_init(&list->mutex,NULL); + pthread_cond_init(&list->cond,NULL); + } -void producer(list_lock_t* list, DataType value) { - perror("This function is not implemented"); +void producer(list_lock_t* list, DataType value) +{ + pthread_mutex_lock(&list->mutex);//加锁 + LNode *Newnode = (LNode*)malloc(sizeof(LNode));//创建新节点 + Newnode->value = value;//给节点赋值 + Newnode->next = list->head;//头插法 + list->head = Newnode; + pthread_mutex_unlock(&list->mutex);//解锁 + pthread_cond_signal(&list->cond);//唤醒 } -void consumer(list_lock_t* list) { - perror("This function is not implemented"); +void consumer(list_lock_t* list) +{ + pthread_mutex_lock(&list->mutex);//加锁 + while(list->head == NULL)//看看链表是不是空的,空的话就睡觉i + { + pthread_cond_wait(&list->cond,&list->mutex);//用conditional Variable来睡觉-->生产者的cond_signal会拍醒消费者 + } + LNode *CunChu = list->head; + list->head = CunChu->next; + free(CunChu); + pthread_mutex_unlock(&list->mutex); + } -int getListSize(list_lock_t* list) { - perror("This function is not implemented"); -} \ No newline at end of file +int getListSize(list_lock_t* list) +{ + pthread_mutex_lock(&list->mutex);//加锁 + int count = 0;//计数 + LNode *p = list->head;//指向头链表,开始走,走一个数一个 + while(p != NULL) + { + count++; + p = p->next; + } + pthread_mutex_unlock(&list->mutex);//解锁 + return count; +} diff --git a/src/lock/lock.c b/src/lock/lock.c index f65675f..15e1aee 100644 --- a/src/lock/lock.c +++ b/src/lock/lock.c @@ -2,14 +2,22 @@ #include -void amountInit(lock_t* Account) { - perror("This function is not implemented"); +void amountInit(lock_t* Account) +{ + Account->amount = 0; + pthread_mutex_init(&Account->mutex, NULL); } -void Income(lock_t* Account, int amount) { - perror("This function is not implemented"); +void Income(lock_t* Account, int amount) +{ + pthread_mutex_lock(&Account->mutex); + Account->amount += amount; + pthread_mutex_unlock(&Account->mutex); } -void Expend(lock_t* Account, int amount) { - perror("This function is not implemented"); -} \ No newline at end of file +void Expend(lock_t* Account, int amount) +{ + pthread_mutex_lock(&Account->mutex); + Account->amount -= amount; + pthread_mutex_unlock(&Account->mutex); +}