2020-01-18 05:43:31 -06:00
|
|
|
//! Atomic intrinsics are implemented using a global lock for now, as Cranelift doesn't support
|
|
|
|
//! atomic operations yet.
|
|
|
|
|
|
|
|
// FIXME implement atomic instructions in Cranelift.
|
|
|
|
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
2020-07-09 07:23:00 -05:00
|
|
|
#[cfg(feature = "jit")]
|
2020-01-18 05:43:31 -06:00
|
|
|
#[no_mangle]
|
2020-08-28 05:10:48 -05:00
|
|
|
pub static mut __cg_clif_global_atomic_mutex: libc::pthread_mutex_t =
|
|
|
|
libc::PTHREAD_MUTEX_INITIALIZER;
|
2020-01-18 05:43:31 -06:00
|
|
|
|
2020-09-29 11:12:23 -05:00
|
|
|
pub(crate) fn init_global_lock(
|
2020-10-01 03:38:23 -05:00
|
|
|
module: &mut impl Module,
|
2020-09-29 11:12:23 -05:00
|
|
|
bcx: &mut FunctionBuilder<'_>,
|
|
|
|
use_jit: bool,
|
|
|
|
) {
|
|
|
|
if use_jit {
|
2020-01-18 05:43:31 -06:00
|
|
|
// When using JIT, dylibs won't find the __cg_clif_global_atomic_mutex data object defined here,
|
2020-09-29 11:12:23 -05:00
|
|
|
// so instead we define it in the cg_clif dylib.
|
2020-01-18 05:43:31 -06:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut data_ctx = DataContext::new();
|
|
|
|
data_ctx.define_zeroinit(1024); // 1024 bytes should be big enough on all platforms.
|
2020-10-01 03:38:23 -05:00
|
|
|
data_ctx.set_align(16);
|
2020-08-28 05:10:48 -05:00
|
|
|
let atomic_mutex = module
|
|
|
|
.declare_data(
|
|
|
|
"__cg_clif_global_atomic_mutex",
|
|
|
|
Linkage::Export,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-01-18 05:43:31 -06:00
|
|
|
module.define_data(atomic_mutex, &data_ctx).unwrap();
|
|
|
|
|
2020-08-28 05:10:48 -05:00
|
|
|
let pthread_mutex_init = module
|
|
|
|
.declare_function(
|
|
|
|
"pthread_mutex_init",
|
|
|
|
Linkage::Import,
|
|
|
|
&cranelift_codegen::ir::Signature {
|
|
|
|
call_conv: module.target_config().default_call_conv,
|
|
|
|
params: vec![
|
|
|
|
AbiParam::new(
|
|
|
|
module.target_config().pointer_type(), /* *mut pthread_mutex_t */
|
|
|
|
),
|
|
|
|
AbiParam::new(
|
|
|
|
module.target_config().pointer_type(), /* *const pthread_mutex_attr_t */
|
|
|
|
),
|
|
|
|
],
|
|
|
|
returns: vec![AbiParam::new(types::I32 /* c_int */)],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-01-18 05:43:31 -06:00
|
|
|
|
|
|
|
let pthread_mutex_init = module.declare_func_in_func(pthread_mutex_init, bcx.func);
|
|
|
|
|
|
|
|
let atomic_mutex = module.declare_data_in_func(atomic_mutex, bcx.func);
|
2020-08-28 05:10:48 -05:00
|
|
|
let atomic_mutex = bcx
|
|
|
|
.ins()
|
|
|
|
.global_value(module.target_config().pointer_type(), atomic_mutex);
|
2020-01-18 05:43:31 -06:00
|
|
|
|
|
|
|
let nullptr = bcx.ins().iconst(module.target_config().pointer_type(), 0);
|
|
|
|
|
|
|
|
bcx.ins().call(pthread_mutex_init, &[atomic_mutex, nullptr]);
|
|
|
|
}
|
|
|
|
|
2020-07-16 07:58:41 -05:00
|
|
|
pub(crate) fn init_global_lock_constructor(
|
2020-10-01 03:38:23 -05:00
|
|
|
module: &mut impl Module,
|
2020-08-28 05:10:48 -05:00
|
|
|
constructor_name: &str,
|
2020-07-16 07:58:41 -05:00
|
|
|
) -> FuncId {
|
|
|
|
let sig = Signature::new(CallConv::SystemV);
|
|
|
|
let init_func_id = module
|
|
|
|
.declare_function(constructor_name, Linkage::Export, &sig)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut ctx = Context::new();
|
|
|
|
ctx.func = Function::with_name_signature(ExternalName::user(0, 0), sig);
|
|
|
|
{
|
|
|
|
let mut func_ctx = FunctionBuilderContext::new();
|
|
|
|
let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx);
|
|
|
|
|
|
|
|
let block = bcx.create_block();
|
|
|
|
bcx.switch_to_block(block);
|
|
|
|
|
2020-09-29 11:12:23 -05:00
|
|
|
crate::atomic_shim::init_global_lock(module, &mut bcx, false);
|
2020-07-16 07:58:41 -05:00
|
|
|
|
|
|
|
bcx.ins().return_(&[]);
|
|
|
|
bcx.seal_all_blocks();
|
|
|
|
bcx.finalize();
|
|
|
|
}
|
2020-08-28 05:10:48 -05:00
|
|
|
module
|
|
|
|
.define_function(
|
|
|
|
init_func_id,
|
|
|
|
&mut ctx,
|
|
|
|
&mut cranelift_codegen::binemit::NullTrapSink {},
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-07-16 07:58:41 -05:00
|
|
|
|
|
|
|
init_func_id
|
|
|
|
}
|
|
|
|
|
2020-10-01 03:38:23 -05:00
|
|
|
pub(crate) fn lock_global_lock(fx: &mut FunctionCx<'_, '_, impl Module>) {
|
2020-08-28 05:10:48 -05:00
|
|
|
let atomic_mutex = fx
|
|
|
|
.cx
|
|
|
|
.module
|
|
|
|
.declare_data(
|
|
|
|
"__cg_clif_global_atomic_mutex",
|
|
|
|
Linkage::Import,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let pthread_mutex_lock = fx
|
|
|
|
.cx
|
|
|
|
.module
|
|
|
|
.declare_function(
|
|
|
|
"pthread_mutex_lock",
|
|
|
|
Linkage::Import,
|
|
|
|
&cranelift_codegen::ir::Signature {
|
|
|
|
call_conv: fx.cx.module.target_config().default_call_conv,
|
|
|
|
params: vec![AbiParam::new(
|
|
|
|
fx.cx.module.target_config().pointer_type(), /* *mut pthread_mutex_t */
|
|
|
|
)],
|
|
|
|
returns: vec![AbiParam::new(types::I32 /* c_int */)],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let pthread_mutex_lock = fx
|
|
|
|
.cx
|
|
|
|
.module
|
|
|
|
.declare_func_in_func(pthread_mutex_lock, fx.bcx.func);
|
2020-01-18 05:43:31 -06:00
|
|
|
|
2020-08-22 09:17:58 -05:00
|
|
|
let atomic_mutex = fx.cx.module.declare_data_in_func(atomic_mutex, fx.bcx.func);
|
2020-08-28 05:10:48 -05:00
|
|
|
let atomic_mutex = fx
|
|
|
|
.bcx
|
|
|
|
.ins()
|
|
|
|
.global_value(fx.cx.module.target_config().pointer_type(), atomic_mutex);
|
2020-01-18 05:43:31 -06:00
|
|
|
|
|
|
|
fx.bcx.ins().call(pthread_mutex_lock, &[atomic_mutex]);
|
|
|
|
}
|
|
|
|
|
2020-10-01 03:38:23 -05:00
|
|
|
pub(crate) fn unlock_global_lock(fx: &mut FunctionCx<'_, '_, impl Module>) {
|
2020-08-28 05:10:48 -05:00
|
|
|
let atomic_mutex = fx
|
|
|
|
.cx
|
|
|
|
.module
|
|
|
|
.declare_data(
|
|
|
|
"__cg_clif_global_atomic_mutex",
|
|
|
|
Linkage::Import,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let pthread_mutex_unlock = fx
|
|
|
|
.cx
|
|
|
|
.module
|
|
|
|
.declare_function(
|
|
|
|
"pthread_mutex_unlock",
|
|
|
|
Linkage::Import,
|
|
|
|
&cranelift_codegen::ir::Signature {
|
|
|
|
call_conv: fx.cx.module.target_config().default_call_conv,
|
|
|
|
params: vec![AbiParam::new(
|
|
|
|
fx.cx.module.target_config().pointer_type(), /* *mut pthread_mutex_t */
|
|
|
|
)],
|
|
|
|
returns: vec![AbiParam::new(types::I32 /* c_int */)],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let pthread_mutex_unlock = fx
|
|
|
|
.cx
|
|
|
|
.module
|
|
|
|
.declare_func_in_func(pthread_mutex_unlock, fx.bcx.func);
|
2020-01-18 05:43:31 -06:00
|
|
|
|
2020-08-22 09:17:58 -05:00
|
|
|
let atomic_mutex = fx.cx.module.declare_data_in_func(atomic_mutex, fx.bcx.func);
|
2020-08-28 05:10:48 -05:00
|
|
|
let atomic_mutex = fx
|
|
|
|
.bcx
|
|
|
|
.ins()
|
|
|
|
.global_value(fx.cx.module.target_config().pointer_type(), atomic_mutex);
|
2020-01-18 05:43:31 -06:00
|
|
|
|
|
|
|
fx.bcx.ins().call(pthread_mutex_unlock, &[atomic_mutex]);
|
|
|
|
}
|