Skip to content

Commit

Permalink
Comments and clean
Browse files Browse the repository at this point in the history
1. Add comments to almost all places (except futex, xchg and cmpxchg)
2. Delete all error checks for concurrent libraries. These libraries are mainly used to test performance of locks, implement error check will introduce irrelavent factors.
2. Clean unused codes and CLion files
3. Create Makefile (without -Wall and -Werror)
TODO: eliminate all warnings.
  • Loading branch information
EmpyreanKnight committed Dec 4, 2017
1 parent 43866cf commit af671c4
Show file tree
Hide file tree
Showing 18 changed files with 290 additions and 831 deletions.
2 changes: 0 additions & 2 deletions .idea/OSLab-P4.iml

This file was deleted.

4 changes: 0 additions & 4 deletions .idea/misc.xml

This file was deleted.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

507 changes: 0 additions & 507 deletions .idea/workspace.xml

This file was deleted.

1 change: 0 additions & 1 deletion cmake-build-debug/CMakeFiles/clion-log.txt

This file was deleted.

13 changes: 0 additions & 13 deletions error.h

This file was deleted.

9 changes: 9 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.8)
project(P4)

set(CMAKE_CXX_STANDARD 11)

find_package (Threads)
set(SOURCE_FILES main.c lock.h counter.h list.h hash.h lock.c counter.c list.c hash.c)
add_executable(P4 ${SOURCE_FILES})
target_link_libraries (P4 ${CMAKE_THREAD_LIBS_INIT})
14 changes: 14 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
make: libcounter.so liblist.so libhash.so

P4:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
cc -lcounter -llist -lhash -L. -o P4 main.c libcounter.so liblist.so libhash.so -lpthread

libcounter.so:
cc -shared -fPIC counter.c lock.h lock.c -o libcounter.so

liblist.so:
cc -shared -fPIC list.c lock.h lock.c -o liblist.so

libhash.so:
cc -shared -fPIC hash.c list.h list.c lock.h lock.c -o libhash.so
40 changes: 15 additions & 25 deletions src/counter.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
#include "counter.h"

extern int user_error;

/**
* Initialization part of counter
* @param c a pointer to a counter
* Initialization part of counter
* @param c pointer to a counter
* @param value Initial value of the counter
*/
void counter_init(counter_t* c, int value) {
void counter_init(counter_t *c, int value) {
c->value = value;
lock_init(&c->lock);
}

/**
* Get the value of the counter
* @param c a pointer to a counter
* @return the value of the counter
* Get the value of the counter
* @param c pointer to a counter
* @return the current value of the counter
*/
int counter_get_value(counter_t* c) {
int counter_get_value(counter_t *c) {
#if defined(LOCK_RWLOCK)
rwlock_rdlock(&c->lock);
#else
Expand All @@ -29,37 +27,29 @@ int counter_get_value(counter_t* c) {
}

/**
* Make the counter's value increase by 1
* @param c a pointer to a counter
* Increase the counter by 1
* @param c pointer to a counter
*/
void counter_increment(counter_t* c) {
void counter_increment(counter_t *c) {
#if defined(LOCK_RWLOCK)
rwlock_wrlock(&c->lock);
#else
lock_acquire(&c->lock);
#endif
if (c->value == INT_MAX) {
user_error = E_DATA_OVERFLOW;
}else {
c->value++;
}
c->value++;
lock_release(&c->lock);
}

/**
* Make the counter's value increase by 1
* @param c a pointer to a counter
* Decrease the counter by 1
* @param c pointer to a counter
*/
void counter_decrement(counter_t* c) {
void counter_decrement(counter_t *c) {
#if defined(LOCK_RWLOCK)
rwlock_wrlock(&c->lock);
#else
lock_acquire(&c->lock);
#endif
if (c->value == INT_MIN) {
user_error = E_DATA_OVERFLOW;
}else {
c->value--;
}
c->value--;
lock_release(&c->lock);
}
4 changes: 2 additions & 2 deletions src/counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
#define P4_COUNTER_H

#include "lock.h"
#define INT_MAX 2147483647
#define INT_MIN (-INT_MAX - 1)

/**
* A concurrent counter type
* All operations except initialization are thread-safe
*/
typedef struct {
int value; /**< internal counter variable */
Expand Down
56 changes: 6 additions & 50 deletions src/hash.c
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
#include "hash.h"

/**
* Initialize hash table with given bucket size
* will call perror to print error message once malloc fail
* Initialize the hash table with given bucket size
* @param hash A pointer to hash table
* @param size designated bucket size
*//*
void hash_init(hash_t *hash, int size, unsigned int threshold) { // hash_init with threshold
hash->lists = malloc(sizeof(list_t)*size);
if (hash->lists == NULL) {
perror("malloc");
return;
}
int i;
hash->bucket_size = size;
hash->threshold = threshold;
for (i = 0; i < size; i++) {
list_init(&hash->lists[i]);
}
//lock_init(&hash->lock);
}*/
void hash_init(hash_t *hash, int size) { // hash_init without threshold
*/
void hash_init(hash_t *hash, int size) {
int i;
hash->bucket_size = size;
hash->lists = malloc(sizeof(list_t)*size);
Expand All @@ -42,7 +27,7 @@ void hash_insert(hash_t *hash, unsigned int key) {

/**
* Delete a key into hash table
* if multiple keys detected in hash table, only delete one of them
* If multiple keys detected in hash table, only delete one of them
* @param hash The pointer to hash table
* @param key The key to be deleted
*/
Expand All @@ -55,15 +40,15 @@ void hash_delete(hash_t *hash, unsigned int key) {
* Find a given key in hash table
* @param hash The pointer to hash table
* @param key The key to find
* @return A pointer to node contains given key, should a into node_t before use.
* @return A pointer to the node with given key, should cast to node_t type before use.
*/
void* hash_lookup(hash_t *hash, unsigned int key) {
int bucket = key % hash->bucket_size;
return list_lookup(&hash->lists[bucket], key);
}

/**
* remove an given hash table and free the pointer of it
* Remove an given hash table and free the pointer of it
* @param hash The pointer to hash table
*/
void* hash_destroy(hash_t *hash) {
Expand All @@ -73,32 +58,3 @@ void* hash_destroy(hash_t *hash) {
}
free(hash->lists);
}

/**
* Check whether the total length of the hash table has exceeded the threshold
* if so resize the hash table and destory the previous one
* @param hash The pointer to a hash table
*/
void hash_resize(hash_t *hash) {
unsigned int i, tmp_key, length = 0;
for (i = 0; i < hash->bucket_size; ++i) {
length += list_count(&hash->lists[i]);
}
if (length < hash->threshold) {
return;
}
hash_t new_hash;
hash_init(&new_hash, length*2, 2*hash->threshold);
for (i = 0; i < hash->bucket_size; ++i) {
list_t tmp_list = hash->lists[i];
node_t *cur = tmp_list.head;
while (cur != NULL) {
tmp_key = cur->key;
hash_insert(&new_hash, tmp_key);
cur = cur->next;
}
}
hash_t* hash_old = hash;
hash = &new_hash;
hash_destroy(hash_old);
}
14 changes: 8 additions & 6 deletions src/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@

#include "list.h"

/**
* The concurrent hash definition
* The hash function is simply module bucket size (but effective)
* This implementation need no more parallel protection since list is already thread-safe
* All operations except initialize and destroy are thread-safe
*/
typedef struct {
list_t *lists;
int bucket_size;
//lock_t lock;
//unsigned int threshold;
list_t *lists; /**< lists for hash buckets */
int bucket_size; /**< the bucket size designated when initialized, can't be changed during use */
} hash_t;

//void hash_init(hash_t *hash, int size, unsigned int threshold);
void hash_init(hash_t *hash, int size);
void hash_insert(hash_t *hash, unsigned int key);
void hash_delete(hash_t *hash, unsigned int key);
void *hash_lookup(hash_t *hash, unsigned int key);
void* hash_destroy(hash_t *hash);
//void hash_resize(hash_t *hash);

#endif //P4_HASH_H
Loading

0 comments on commit af671c4

Please sign in to comment.