Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 78 additions & 9 deletions src/lock/hash_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,87 @@
#include <stdio.h>
#include <stdlib.h>

void hashInit(hash_lock_t* bucket) {
perror("This function is not implemented");
void hashInit(hash_lock_t* bucket)
{
for(int i = 0;i<HASHNUM;i++)
{
bucket->table[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");
}
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;
}
49 changes: 40 additions & 9 deletions src/lock/list_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,49 @@
#include <stdio.h>
#include <stdlib.h>

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");
}
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;
}
22 changes: 15 additions & 7 deletions src/lock/lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

#include <stdio.h>

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");
}
void Expend(lock_t* Account, int amount)
{
pthread_mutex_lock(&Account->mutex);
Account->amount -= amount;
pthread_mutex_unlock(&Account->mutex);
}