-
Notifications
You must be signed in to change notification settings - Fork 0
Queue implementation #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
teadetime
wants to merge
10
commits into
main
Choose a base branch
from
queue-implementation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ab0dd05
base queue
teadetime f664f52
threaded queue implementation
teadetime ecd5781
added sempahore
teadetime c61d21b
void add and semaphore test
teadetime 2e45c80
runtime wip
teadetime 21be1b7
Added block freeing function
teadetime b1bf4f5
miner and node_block runtime
teadetime 4e3614a
concurrent miner and block processor
teadetime 19cc6ee
set difficulty higher
teadetime 995b745
Shell concurrent
teadetime File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <semaphore.h> | ||
| #include "pthread.h" | ||
| #include "queue.h" | ||
| #include "base_tx.h" | ||
| #include "base_block.h" | ||
| #include <unistd.h> | ||
|
|
||
| Queue *queue_init(){ | ||
| Queue *new_q = malloc(sizeof(Queue)); | ||
| new_q->head = NULL; | ||
| new_q->tail = NULL; | ||
| //INitialize sepmaphores and lock | ||
| sem_init(&new_q->sem_len, 0, 0); | ||
| if (pthread_mutex_init(&new_q->lock, NULL) != 0) { | ||
| printf("\n mutex init has failed\n"); | ||
| return NULL; | ||
| } | ||
| //pthread_mutex_unlock(&new_q->lock); | ||
| return new_q; | ||
| } | ||
|
|
||
| void queue_destroy(Queue *existing_q){ | ||
| pthread_mutex_lock(&existing_q->lock); // Maybe we don't need to lock if we are destroying? | ||
| pthread_mutex_destroy(&existing_q->lock); | ||
| QueueItem *cur_item = existing_q->head; | ||
| QueueItem *next_item = NULL; | ||
| while(cur_item != NULL){ | ||
| next_item = cur_item->next; | ||
| free(cur_item); | ||
| cur_item = next_item; | ||
| } | ||
| free(existing_q); | ||
| } | ||
|
|
||
| int queue_add_void(Queue *existing_q, void *new_item){ | ||
| pthread_mutex_lock(&existing_q->lock); | ||
| QueueItem *new_entry = malloc(sizeof(QueueItem)); | ||
| new_entry->item = new_item; | ||
| new_entry->next = NULL; | ||
| if(!existing_q->tail && !existing_q->head){ | ||
| existing_q->tail = new_entry; | ||
| existing_q->head = new_entry; | ||
| } | ||
| else{ | ||
| existing_q->tail->next = new_entry; | ||
| existing_q->tail = new_entry; | ||
| } | ||
| pthread_mutex_unlock(&existing_q->lock); | ||
| int ret = sem_post(&existing_q->sem_len); | ||
| return 0; | ||
| } | ||
|
|
||
| void *queue_pop_void(Queue *existing_q){ | ||
| sem_wait(&existing_q->sem_len); | ||
| pthread_mutex_lock(&existing_q->lock); | ||
| if(!existing_q->head){ | ||
| fprintf(stderr, "Popping off empty Queue"); | ||
| exit(1); | ||
| } | ||
|
|
||
| QueueItem *popped_item = existing_q->head; | ||
| existing_q->head = existing_q->head->next; | ||
| // Check to see if tail should be set to Null | ||
| if(!existing_q->head){ | ||
| existing_q->tail = NULL; | ||
| } | ||
| void *ret = popped_item->item; | ||
| free(popped_item); | ||
| pthread_mutex_unlock(&existing_q->lock); | ||
| return ret; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| #pragma once | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <pthread.h> | ||
| #include <semaphore.h> | ||
| #include "base_tx.h" | ||
| #include "base_block.h" | ||
|
|
||
| typedef struct QueueItem{ | ||
| void *item; | ||
| struct QueueItem *next; | ||
| } QueueItem; | ||
|
|
||
| typedef struct Queue { | ||
| QueueItem *head; | ||
| QueueItem *tail; | ||
| pthread_mutex_t lock; | ||
| sem_t sem_len; | ||
| } Queue; | ||
|
|
||
| Queue *queue_init(); | ||
|
|
||
| void queue_destroy(Queue *exisiting_q); | ||
|
|
||
| int queue_add_void(Queue *existing_q, void *new_item); | ||
| void *queue_pop_void(Queue *existing_q); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #pragma once | ||
| #include "queue.h" | ||
| #include "pthread.h" | ||
| #include "semaphore.h" | ||
|
|
||
| typedef struct Globals { | ||
| Queue *queue_block; | ||
| Queue *queue_tx; | ||
| pthread_mutex_t utxo_pool_lock; | ||
| pthread_mutex_t utxo_to_tx_lock; | ||
| pthread_mutex_t blockchain_lock; | ||
| pthread_mutex_t wallet_pool_lock; | ||
| pthread_mutex_t key_pool_lock; | ||
| pthread_mutex_t mempool_lock; | ||
| int *miner_update; | ||
| pthread_mutex_t miner_update_lock; | ||
| } Globals; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to malloc this int?