Skip to content
Snippets Groups Projects
Commit 97ee8ac1 authored by Avi Kivity's avatar Avi Kivity
Browse files

mutex: improve compatibility with pthread_mutex_t initializers

pthread_mutex_t has a 32-bit field, __kind, at offset 16.  Non-standard
static initializers set this field to a nonzero value, which can corrupt
fields in our implementation.

Rearrange field layout so we have a hole in that position.  To keep the
structure size small enough so that condvar will still fit in
pthread_condvar_t, we need to change the size of the _depth field to
16 bits.
parent e25fc7e7
No related branches found
No related tags found
No related merge requests found
#include <osv/mutex.h>
#include <sched.hh>
#include "arch.hh"
#include <pthread.h>
#include <cassert>
static_assert(sizeof(mutex) <= sizeof(pthread_mutex_t), "mutex too big");
static_assert(offsetof(mutex, _hole_for_pthread_compatiblity) == 16, "mutex hole in wrong place");
struct waiter {
struct waiter* next;
......
......@@ -3,6 +3,7 @@
#include <stdbool.h>
#include <string.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
......@@ -42,13 +43,14 @@ void spinlock::unlock()
// Mutex:
typedef struct mutex {
spinlock_t _wait_lock;
unsigned _depth;
void *_owner;
struct wait_list {
struct waiter *first;
struct waiter *last;
} _wait_list;
uint32_t _hole_for_pthread_compatiblity; // pthread_mutex_t's __kind
spinlock_t _wait_lock;
uint16_t _depth;
void *_owner;
#ifdef __cplusplus
// additional convenience methods for C++
inline mutex();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment