Skip to content
Draft
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
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ export COND_VAR=1
.SECONDARY: $(OBJS)
.PHONY: all clean format

all:
ifeq ($(BUILD_MUSL), 1)
all: musl_mutex $(DIR) include/topology.h $(SOS) $(SHS)
else
all: $(DIR) include/topology.h $(SOS) $(SHS)
endif

no_cond_var: COND_VAR=0
no_cond_var: all

%.so: obj/CLHT/libclht.a obj/
%.so: musl_mutex obj/CLHT/libclht.a obj/
mkdir -p lib/
$(MAKE) -C src/ ../lib/$@

Expand All @@ -31,6 +36,9 @@ obj/:
$(DIR):
mkdir -p $@

musl_mutex:
$(MAKE) -C src/musl_mutex

obj/CLHT:
cd obj/ && ([ -d CLHT ] || git clone https://github.com/HugoGuiroux/CLHT.git)
mkdir -p obj/CLHT/external/lib/
Expand Down
53 changes: 27 additions & 26 deletions Makefile.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,34 @@
# A = algorithm name, lowercase, without space (must match the src/*.c and src/*.h name)
# S = waiting strategy. original = hardcoded in the algorithm (see README), otherwise spinlock/spin_then_park/park

ALGORITHMS=mcs_spinlock \
mcsepfl_original \
mcs_spin_then_park \
mcstp_original \
ALGORITHMS=muslmutex_original \
spinlock_original \
spinlockepfl_original \
malthusian_spinlock \
malthusian_spin_then_park \
ttas_original \
ttasepfl_original \
ticket_original \
ticketepfl_original \
clh_spinlock \
clh_spin_then_park \
clhepfl_original \
backoff_original \
empty_original \
hmcs_original \
mcs_spinlock \
pthreadinterpose_original \
pthreadadaptive_original \
concurrency_original \
htlockepfl_original \
alockepfl_original \
hmcs_original \
hyshmcs_original \
cbomcs_spinlock \
cbomcs_spin_then_park \
cptltkt_original \
ctkttkt_original \
partitioned_original \
mutexee_original
#mcsepfl_original \
#mcs_spin_then_park \
#mcstp_original \
#spinlockepfl_original \
#malthusian_spinlock \
#malthusian_spin_then_park \
#ttas_original \
#ttasepfl_original \
#ticket_original \
#ticketepfl_original \
#clh_spinlock \
#clh_spin_then_park \
#clhepfl_original \
#backoff_original \
#concurrency_original \
#htlockepfl_original \
#alockepfl_original \
#hyshmcs_original \
#cbomcs_spinlock \
#cbomcs_spin_then_park \
#cptltkt_original \
#ctkttkt_original \
#partitioned_original \
#mutexee_original
18 changes: 18 additions & 0 deletions ci-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh

set -eux

#Discard all uncommited changes
git checkout -f

#Build LiTL
nproc="$(nproc)"
make BUILD_MUSL=1 no_cond_var -j "$nproc"

#Delete source files, keep only .so libraries and .sh scripts for use those libraries
if [ "${1:-}" = "delete_sources" ]; then
find . -mindepth 1 ! -path "./.git/*" ! -name ".git" ! -name ".gitignore" ! -name ".clang-format" \
! -path "./lib/*" ! -name "lib" \
! -name "*.sh" \
-exec rm -rf {} +
fi
122 changes: 122 additions & 0 deletions include/muslmutex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Hugo Guiroux <hugo.guiroux at gmail dot com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of his software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef __MUSL_MUTEX_H__
#define __MUSL_MUTEX_H__

#define LOCK_ALGORITHM "MUSL"
#define NEED_CONTEXT 0
#define SUPPORT_WAITING 0

// Define needed types before including alltypes_make.h
// #define __NEED_pthread_cond_t
// #define __NEED_pthread_condattr_t

#include "../src/musl_mutex/alltypes_make.h"
#include "padding.h" // for glibc pthread_cond_t

// typedef struct musl_mutex {
// // #if COND_VAR
// // pthread_mutex_t posix_lock;
// // char __pad[pad_to_cache_line(sizeof(pthread_mutex_t))];
// // #endif
// __pthread_mutex_t lock;
// } musl_mutex_t __attribute__((aligned(L_CACHE_LINE_SIZE)));

typedef __pthread_mutex_t musl_mutex_t;
typedef __pthread_cond_t musl_cond_t;
// typedef void *musl_context_t; // Unused, take the less space
// as possible
typedef void *musl_context_t;
typedef musl_mutex_t lock_mutex_t;
typedef musl_context_t lock_context_t;
typedef musl_cond_t lock_cond_t;

//

// typedef __pthread_mutex_t musl_mutex_t;
// typedef __pthread_mutexattr_t musl_mutexattr_t;

// typedef pthread_mutex_t musl_mutex_t;
// typedef pthread_cond_t musl_cond_t;
// typedef void *musl_context_t;

// typedef musl_mutex_t lock_mutex_t;
// typedef musl_context_t lock_context_t;

// typedef void *musl_context_t; // Unused, take the less space as possible
// typedef void *lock_context_t; // Required for interpose.c

//Decls
musl_mutex_t *musl_mutex_create(const __pthread_mutexattr_t *attr);
int musl_mutex_destroy(musl_mutex_t *lock);


// int musl_cond_init(musl_cond_t *cond, const pthread_condattr_t *attr);
// int __pthread_musl_cond_init(musl_cond_t *restrict c, const pthread_condattr_t *restrict a);
// // int musl_cond_timedwait(musl_cond_t *cond, musl_mutex_t *lock,
// // musl_context_t *me, const struct timespec *ts);
// int __pthread_musl_cond_timedwait(musl_cond_t *restrict c, musl_mutex_t *restrict m, musl_context_t *restrict me, const struct timespec *restrict ts);
// // int musl_cond_wait(musl_cond_t *cond, musl_mutex_t *lock, musl_context_t *me);
// int __pthread_musl_cond_wait(musl_cond_t *restrict c, musl_mutex_t *restrict m, musl_context_t *restrict me);

// // int musl_cond_signal(musl_cond_t *cond);
// int __pthread_musl_cond_signal(musl_cond_t *cond);
// // int musl_cond_broadcast(musl_cond_t *cond);
// int __pthread_musl_cond_broadcast(musl_cond_t *cond);
// // int musl_cond_destroy(musl_cond_t *cond);
// int __pthread_musl_cond_destroy(musl_cond_t *c);

void musl_thread_start(void);
void musl_thread_exit(void);
void musl_application_init(void);
void musl_application_exit(void);
//unused


//Defines
// From musl_mutex/pthread_musl.h
#define lock_mutex_lock __pthread_musl_mutex_lock
#define lock_mutex_trylock __pthread_musl_mutex_trylock
#define lock_mutex_unlock __pthread_musl_mutex_unlock
// #define musl_mutex_init __pthread_mutex_init

// From here
#define lock_mutex_create musl_mutex_create
#define lock_mutex_destroy musl_mutex_destroy

//Unused
#define lock_cond_init __pthread_musl_cond_init
#define lock_cond_timedwait __pthread_musl_cond_timedwait
#define lock_cond_wait __pthread_musl_cond_wait
#define lock_cond_signal __pthread_musl_cond_signal
#define lock_cond_broadcast __pthread_musl_cond_broadcast
#define lock_cond_destroy __pthread_musl_cond_destroy
#define lock_thread_start musl_thread_start
#define lock_thread_exit musl_thread_exit
#define lock_application_init musl_application_init
#define lock_application_exit musl_application_exit
#define lock_init_context musl_init_context
//unused

#endif // __MUSL_MUTEX_H__
4 changes: 2 additions & 2 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include ../Makefile.config

LDFLAGS=-L../obj/CLHT/external/lib -L../obj/CLHT -Wl,--whole-archive -Wl,--version-script=interpose.map -lsspfd -lssmem -lclht -Wl,--no-whole-archive -lrt -lm -ldl -lpapi -m64 -pthread
CFLAGS=-I../include/ -I../obj/CLHT/include/ -I../obj/CLHT/external/include/ -fPIC -Wall -Werror -O3 -g
CFLAGS=-I../include/ -I../obj/CLHT/include/ -I../obj/CLHT/external/include/ -fPIC -Wall -O3 -g -Wno-flexible-array-member-not-at-end

# Keep objects files
.PRECIOUS: %.o
Expand All @@ -19,4 +19,4 @@ CFLAGS=-I../include/ -I../obj/CLHT/include/ -I../obj/CLHT/external/include/ -fPI

.SECONDEXPANSION:
../lib/lib%.so: ../obj/%/interpose.o ../obj/%/utils.o $$(subst algo,%,../obj/algo/algo.o)
$(CC) -shared -o $@ $^ $(LDFLAGS)
$(CC) -shared -o $@ $^ -L ./musl_mutex -lmusl_mutex $(LDFLAGS)
9 changes: 9 additions & 0 deletions src/empty.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,39 +49,48 @@ int empty_mutex_lock(empty_mutex_t *impl, empty_context_t *UNUSED(me)) {
}

int empty_mutex_trylock(empty_mutex_t *impl, empty_context_t *UNUSED(me)) {
// fprintf(stderr, "empty_mutex_trylock\n");
return REAL(pthread_mutex_trylock)(impl);
}
void empty_mutex_unlock(empty_mutex_t *impl, empty_context_t *UNUSED(me)) {
// fprintf(stderr, "empty_mutex_unlock\n");
REAL(pthread_mutex_unlock)(impl);
}

int empty_mutex_destroy(empty_mutex_t *lock) {
// fprintf(stderr, "empty_mutex_destroy\n");
return REAL(pthread_mutex_destroy)(lock);
}

int empty_cond_init(empty_cond_t *cond, const pthread_condattr_t *attr) {
// fprintf(stderr, "empty_cond_init\n");
return REAL(pthread_cond_init)(cond, attr);
}

int empty_cond_timedwait(empty_cond_t *cond, empty_mutex_t *lock,
empty_context_t *me, const struct timespec *ts) {
// fprintf(stderr, "empty_cond_timedwait\n");
return REAL(pthread_cond_timedwait)(cond, lock, ts);
}

int empty_cond_wait(empty_cond_t *cond, empty_mutex_t *lock,
empty_context_t *UNUSED(me)) {
// fprintf(stderr, "empty_cond_wait\n");
return REAL(pthread_cond_wait)(cond, lock);
}

int empty_cond_signal(empty_cond_t *cond) {
// fprintf(stderr, "empty_cond_signal\n");
return REAL(pthread_cond_signal)(cond);
}

int empty_cond_broadcast(empty_cond_t *cond) {
// fprintf(stderr, "empty_cond_broadcast\n");
return REAL(pthread_cond_broadcast)(cond);
}

int empty_cond_destroy(empty_cond_t *cond) {
// fprintf(stderr, "empty_cond_destroy\n");
return REAL(pthread_cond_destroy)(cond);
}

Expand Down
4 changes: 3 additions & 1 deletion src/interpose.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@
#include <partitioned.h>
#elif defined(MUTEXEE)
#include <mutexee.h>
#elif defined(MUSLMUTEX)
#include <muslmutex.h>
#else
#error "No lock algorithm known"
#endif
Expand Down Expand Up @@ -697,4 +699,4 @@ int pthread_rwlock_unlock(pthread_rwlock_t *rwlock) {
#else
assert(0 && "rwlock not supported without indirection");
#endif
}
}
2 changes: 1 addition & 1 deletion src/interpose.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ extern int (*REAL(pthread_rwlock_trywrlock))(pthread_rwlock_t *lock);
extern int (*REAL(pthread_rwlock_unlock))(pthread_rwlock_t *lock);

// rdwr locks
#endif // __INTERPOSE_H__
#endif // __INTERPOSE_H__
5 changes: 5 additions & 0 deletions src/interpose.map
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ GLIBC_2.3.2 {
pthread_cond_wait;
pthread_cond_timedwait;
} GLIBC_2.2.5;

GLIBC_2.39 {
global:
pthread_create;
} GLIBC_2.3.2;
6 changes: 3 additions & 3 deletions src/mcstp.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* is preempted.
* Indeed, a thread A can overtake another thread B behind which it has been
* waiting for a long time.
* The waiting thread A will first yield its CPU (via pthread_yield) in order to
* The waiting thread A will first yield its CPU (via sched_yield) in order to
* create an opportunity for the preempted thread (lock holder or B) to make
* progress.
* If this is not enough, T will overtake the predecessor thread (B) by marking
Expand Down Expand Up @@ -144,7 +144,7 @@ int mcs_tp_mutex_trylock(mcs_tp_mutex_t *impl, mcs_tp_node_t *me) {
goto success;
} else if (me->status == FAILED) {
if (GET_TIME() - impl->cs_start_time > MAX_CS_TIME)
pthread_yield();
sched_yield();

me->last_lock = impl;
return EBUSY;
Expand All @@ -164,7 +164,7 @@ int mcs_tp_mutex_trylock(mcs_tp_mutex_t *impl, mcs_tp_node_t *me) {
}

if (GET_TIME() - impl->cs_start_time > MAX_CS_TIME)
pthread_yield();
sched_yield();

me->last_lock = impl;
return EBUSY;
Expand Down
Loading