Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
exercises
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ISC2
pco
exercises
Commits
08e82543
Commit
08e82543
authored
1 year ago
by
iliya
Browse files
Options
Downloads
Patches
Plain Diff
feat: using provided wrappers, tasks are being launched in parallel
parent
4bf79a5e
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
mutex_barriers/ex3/Ex3_etu.c
+19
-26
19 additions, 26 deletions
mutex_barriers/ex3/Ex3_etu.c
with
19 additions
and
26 deletions
mutex_barriers/ex3/Ex3_etu.c
+
19
−
26
View file @
08e82543
#include
"thread_wrapper.h"
#include
"thread_wrapper.h"
//
#include <assert.h>
#include
<assert.h>
#include
<pthread.h>
#include
<pthread.h>
#include
<stdbool.h>
#include
<stdbool.h>
#include
<stdio.h>
#include
<stdio.h>
...
@@ -20,7 +20,7 @@ typedef struct {
...
@@ -20,7 +20,7 @@ typedef struct {
int
*
ptr
;
int
*
ptr
;
int
size
;
int
size
;
int
capacity
;
int
capacity
;
pthread_mutex_t
lock
;
pthread_mutex_t
*
lock
;
}
stack_t
;
}
stack_t
;
typedef
struct
{
typedef
struct
{
...
@@ -29,20 +29,14 @@ typedef struct {
...
@@ -29,20 +29,14 @@ typedef struct {
}
thr_params
;
}
thr_params
;
bool
stack_create
(
stack_t
*
s
,
int
max_size
)
{
bool
stack_create
(
stack_t
*
s
,
int
max_size
)
{
if
(
max_size
<
1
)
{
assert
(
max_size
>=
1
);
fprintf
(
stderr
,
"Stack size has to be equal to or greater than 1"
);
return
false
;
}
pthread_mutexattr_t
attr
;
pthread_mutexattr_init
(
&
attr
);
pthread_mutexattr_settype
(
&
attr
,
PTHREAD_MUTEX_ERRORCHECK
);
pthread_mutex_init
(
&
s
->
lock
,
&
attr
);
s
->
lock
=
mutex_create
();
assert
(
s
->
lock
!=
NULL
);
// pthread_mutexattr_destroy(&attr);
s
->
ptr
=
malloc
(
max_size
*
sizeof
(
int
));
s
->
ptr
=
malloc
(
max_size
*
sizeof
(
int
));
assert
(
s
->
ptr
!=
NULL
);
s
->
capacity
=
max_size
;
s
->
capacity
=
max_size
;
s
->
size
=
0
;
s
->
size
=
0
;
...
@@ -53,11 +47,10 @@ bool stack_is_empty(stack_t *s) { return s->size == 0; }
...
@@ -53,11 +47,10 @@ bool stack_is_empty(stack_t *s) { return s->size == 0; }
void
stack_push
(
stack_t
*
s
,
int
val
)
{
void
stack_push
(
stack_t
*
s
,
int
val
)
{
if
(
s
->
size
<
s
->
capacity
)
{
if
(
s
->
size
<
s
->
capacity
)
{
pthread_
mutex_lock
(
&
s
->
lock
);
mutex_lock
(
s
->
lock
);
s
->
ptr
[(
s
->
capacity
-
1
)
-
s
->
size
]
=
val
;
s
->
ptr
[(
s
->
capacity
-
1
)
-
s
->
size
]
=
val
;
s
->
size
++
;
s
->
size
++
;
pthread_mutex_unlock
(
&
s
->
lock
);
mutex_unlock
(
s
->
lock
);
// fprintf(stderr, "%d has been inserted\n", val);
}
else
{
}
else
{
fprintf
(
stderr
,
"Stack is full
\n
"
);
fprintf
(
stderr
,
"Stack is full
\n
"
);
}
}
...
@@ -65,28 +58,28 @@ void stack_push(stack_t *s, int val) {
...
@@ -65,28 +58,28 @@ void stack_push(stack_t *s, int val) {
// Returns -1 in case of an error
// Returns -1 in case of an error
int
stack_pop
(
stack_t
*
s
)
{
int
stack_pop
(
stack_t
*
s
)
{
if
(
stack_is_empty
(
s
))
{
assert
(
!
stack_is_empty
(
s
));
fprintf
(
stderr
,
"Stack is empty
\n
"
);
return
-
1
;
}
pthread_mutex_lock
(
&
s
->
lock
);
// pthread_mutex_lock(&s->lock);
mutex_lock
(
s
->
lock
);
int
ret
=
s
->
ptr
[
s
->
capacity
-
s
->
size
];
int
ret
=
s
->
ptr
[
s
->
capacity
-
s
->
size
];
s
->
size
--
;
s
->
size
--
;
pthread_
mutex_unlock
(
&
s
->
lock
);
mutex_unlock
(
s
->
lock
);
return
ret
;
return
ret
;
}
}
void
stack_destroy
(
stack_t
*
s
)
{
void
stack_destroy
(
stack_t
*
s
)
{
if
(
s
->
ptr
!=
NULL
)
{
mutex_destroy
(
s
->
lock
);
pthread_mutex_destroy
(
&
s
->
lock
);
assert
(
s
->
ptr
!=
NULL
);
free
(
s
->
ptr
);
free
(
s
->
ptr
);
}
}
}
void
*
test_stack
(
void
*
data
)
{
void
*
test_stack
(
void
*
data
)
{
fprintf
(
stderr
,
"thread n°%lu, waiting...
\n
"
,
pthread_self
());
bar_wait
(
b
);
bar_wait
(
b
);
fprintf
(
stderr
,
"launching task for thread id n°%lu
\n
"
,
pthread_self
());
thr_params
*
p
=
(
thr_params
*
)
data
;
thr_params
*
p
=
(
thr_params
*
)
data
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment