Skip to content

Commit

Permalink
Mutex fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
djordje200179 committed Sep 9, 2023
1 parent f184830 commit 1fe081f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Software/h/threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ typedef struct kmutex* mtx_t;

enum {
mtx_plain,
mtx_timed,
mtx_recursive,
mtx_timed
};

int mtx_init(mtx_t* mutex, int type);
Expand Down
41 changes: 30 additions & 11 deletions Software/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@
#include <stdbool.h>
#include <threads.h>

mtx_t mtx;

int digit_thread(void* arg) {
uintptr_t id = (uintptr_t)arg;

ssds_set_digit(id, 0);

mtx_lock(&mtx);

ssds_set_digit(id, 1);

mtx_unlock(&mtx);

ssds_set_digit(id, 2);

Expand All @@ -14,18 +24,27 @@ int digit_thread(void* arg) {
void main() {
ssds_on();

ssds_set_digit(3, 0);
ssds_set_digit(0, 0);

mtx_init(&mtx, mtx_recursive);

ssds_set_digit(0, 1);

mtx_lock(&mtx);
mtx_lock(&mtx);

ssds_set_digit(0, 2);

thrd_t thread;
thrd_create(&thread, digit_thread, (void*)2);

ssds_set_digit(1, 0);

thrd_yield();

thrd_t threads[4];
ssds_set_digit(1, 1);

uint8_t created = 0;
for (int i = 0; i < 2; i++) {
int res = thrd_create(&threads[i], digit_thread, (void*)(uintptr_t)i);
if (res == thrd_success)
created++;
else
ssds_set_digit(2, res);
mtx_unlock(&mtx);

ssds_set_digit(3, created);
}
ssds_set_digit(1, 2);
}

0 comments on commit 1fe081f

Please sign in to comment.