Skip to content

Commit

Permalink
Remove posix interop (#440)
Browse files Browse the repository at this point in the history
Fixes #324
  • Loading branch information
mvicsokolova authored Jun 21, 2024
1 parent 814a5b7 commit a70defe
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 120 deletions.
106 changes: 71 additions & 35 deletions atomicfu/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile

plugins {
Expand All @@ -12,12 +13,10 @@ plugins {
ext {
nativeMainSets = []
nativeTestSets = []
nativeCompilations = []

addNative = { preset ->
nativeMainSets.add(preset.compilations['main'].kotlinSourceSets.first())
nativeTestSets.add(preset.compilations['test'].kotlinSourceSets.first())
nativeCompilations.add(preset.compilations['main'])
}
}

Expand Down Expand Up @@ -124,43 +123,76 @@ kotlin {
}
}

// Support of all non-deprecated targets from official tier list: https://kotlinlang.org/docs/native-target-support.html
kotlin {
targets {
// Support of all non-deprecated targets from official tier list: https://kotlinlang.org/docs/native-target-support.html

// Tier #1
addTarget(presets.linuxX64)
addTarget(presets.macosX64)
addTarget(presets.macosArm64)
addTarget(presets.iosSimulatorArm64)
addTarget(presets.iosX64)

// Tier #2
addTarget(presets.linuxArm64)
addTarget(presets.watchosSimulatorArm64)
addTarget(presets.watchosX64)
addTarget(presets.watchosArm32)
addTarget(presets.watchosArm64)
addTarget(presets.tvosSimulatorArm64)
addTarget(presets.tvosX64)
addTarget(presets.tvosArm64)
addTarget(presets.iosArm64)


// Tier #3
addTarget(presets.androidNativeArm32)
addTarget(presets.androidNativeArm64)
addTarget(presets.androidNativeX86)
addTarget(presets.androidNativeX64)
addTarget(presets.mingwX64)
addTarget(presets.watchosDeviceArm64)
// Tier 1
macosX64()
macosArm64()
iosSimulatorArm64()
iosX64()

// Tier 2
linuxX64()
linuxArm64()
watchosSimulatorArm64()
watchosX64()
watchosArm32()
watchosArm64()
tvosSimulatorArm64()
tvosX64()
tvosArm64()
iosArm64()

// Tier 3
androidNativeArm32()
androidNativeArm64()
androidNativeX86()
androidNativeX64()
mingwX64()
watchosDeviceArm64()

applyDefaultHierarchyTemplate { target ->
target.group("native") { nativeTargets ->
nativeTargets.group("nativeUnixLike") {
it.withLinux()
it.withApple()
}
nativeTargets.group("androidNative32Bit") {
it.withAndroidNativeX86()
// This is a WA: in KotlinHierarchyBuilderImpl.withAndroidNativeArm32 the KonanTarget equals ANDROID_X86 instead of ANDROID_ARM32
it.withCompilations { compilation ->
compilation.target instanceof KotlinNativeTarget && (compilation.target as KotlinNativeTarget).konanTarget.name == "android_arm32"
}
}
nativeTargets.group("androidNative64Bit") {
it.withAndroidNativeArm64()
it.withAndroidNativeX64()
}
}
}

sourceSets {
nativeMain { dependsOn commonMain }

nativeTest {}

nativeUnixLikeMain {
kotlin.srcDir("src/nativeUnixLikeMain/kotlin")
dependsOn nativeMain
}

androidNative32BitMain {
kotlin.srcDir("src/androidNative32BitMain/kotlin")
dependsOn nativeMain
}

androidNative64BitMain {
kotlin.srcDir("src/androidNative64BitMain/kotlin")
dependsOn nativeMain
}

mingwX64Main { dependsOn nativeMain }

configure(nativeMainSets) {
dependsOn nativeMain
}
Expand All @@ -170,10 +202,14 @@ kotlin {
}
}

configure(nativeCompilations) {
cinterops {
interop {
defFile 'src/nativeInterop/cinterop/interop.def'
// atomicfu-cinterop-interop.klib with an empty interop.def file will still be published for compatibility reasons (see KT-68411)
// This block can be removed when this issue in K/N compiler is resolved: KT-60874
targets.withType(KotlinNativeTarget).configureEach { nativeTarget ->
nativeTarget.compilations.configureEach { nativeCompilation ->
nativeCompilation.cinterops {
interop {
defFile file('src/nativeInterop/cinterop/interop.def')
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package kotlinx.atomicfu.locks

import kotlinx.cinterop.*
import platform.posix.*
import kotlin.concurrent.Volatile

public actual class NativeMutexNode {

@Volatile
private var isLocked = false
private val pMutex = nativeHeap.alloc<pthread_mutex_t>().apply { pthread_mutex_init(ptr, null) }
private val pCond = nativeHeap.alloc<pthread_cond_t>().apply { pthread_cond_init(ptr, null) }

internal actual var next: NativeMutexNode? = null

actual fun lock() {
pthread_mutex_lock(pMutex.ptr)
while (isLocked) { // wait till locked are available
pthread_cond_wait(pCond.ptr, pMutex.ptr)
}
isLocked = true
pthread_mutex_unlock(pMutex.ptr)
}

actual fun unlock() {
pthread_mutex_lock(pMutex.ptr)
isLocked = false
pthread_cond_broadcast(pCond.ptr)
pthread_mutex_unlock(pMutex.ptr)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package kotlinx.atomicfu.locks

import kotlinx.cinterop.*
import platform.posix.*
import kotlin.concurrent.Volatile

public actual class NativeMutexNode {

@Volatile
private var isLocked = false
private val pMutex = nativeHeap.alloc<pthread_mutex_t>().apply { pthread_mutex_init(ptr, null) }
private val pCond = nativeHeap.alloc<pthread_cond_t>().apply { pthread_cond_init(ptr, null) }

internal actual var next: NativeMutexNode? = null

actual fun lock() {
pthread_mutex_lock(pMutex.ptr)
while (isLocked) { // wait till locked are available
pthread_cond_wait(pCond.ptr, pMutex.ptr)
}
isLocked = true
pthread_mutex_unlock(pMutex.ptr)
}

actual fun unlock() {
pthread_mutex_lock(pMutex.ptr)
isLocked = false
pthread_cond_broadcast(pCond.ptr)
pthread_mutex_unlock(pMutex.ptr)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package kotlinx.atomicfu.locks

import kotlinx.cinterop.*
import platform.posix.*
import kotlin.concurrent.Volatile

public actual class NativeMutexNode {

@Volatile
private var isLocked = false
private val pMutex = nativeHeap.alloc<pthread_mutex_tVar>().apply { pthread_mutex_init(ptr, null) }
private val pCond = nativeHeap.alloc<pthread_cond_tVar>().apply { pthread_cond_init(ptr, null) }

internal actual var next: NativeMutexNode? = null

actual fun lock() {
pthread_mutex_lock(pMutex.ptr)
while (isLocked) { // wait till locked are available
pthread_cond_wait(pCond.ptr, pMutex.ptr)
}
isLocked = true
pthread_mutex_unlock(pMutex.ptr)
}

actual fun unlock() {
pthread_mutex_lock(pMutex.ptr)
isLocked = false
pthread_cond_broadcast(pCond.ptr)
pthread_mutex_unlock(pMutex.ptr)
}
}
33 changes: 0 additions & 33 deletions atomicfu/src/nativeInterop/cinterop/interop.def
Original file line number Diff line number Diff line change
@@ -1,33 +0,0 @@
---
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

typedef struct lock_support {
volatile int locked;
pthread_mutex_t mutex;
pthread_cond_t cond;
} lock_support_t;

lock_support_t* lock_support_init() {
lock_support_t * ls = (lock_support_t *) malloc(sizeof(lock_support_t));
ls->locked = 0;
pthread_mutex_init(&ls->mutex, NULL);
pthread_cond_init(&ls->cond, NULL);
return ls;
}
void lock(lock_support_t* ls) {
pthread_mutex_lock(&ls->mutex);
while (ls->locked == 1) { // wait till locked are available
pthread_cond_wait(&ls->cond, &ls->mutex);
}
ls->locked = 1;
pthread_mutex_unlock(&ls->mutex);
}

void unlock(lock_support_t* ls) {
pthread_mutex_lock(&ls->mutex);
ls->locked = 0;
pthread_cond_broadcast(&ls->cond);
pthread_mutex_unlock(&ls->mutex);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package kotlinx.atomicfu.locks

public expect class NativeMutexNode() {
internal var next: NativeMutexNode?

public fun lock()

public fun unlock()
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ private val mutexPool by lazy { MutexPool(INITIAL_POOL_CAPACITY) }
class MutexPool(capacity: Int) {
private val top = AtomicReference<NativeMutexNode?>(null)

private val mutexes = Array<NativeMutexNode>(capacity) { NativeMutexNode() }
private val mutexes = Array(capacity) { NativeMutexNode() }

init {
// Immediately form a stack
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package kotlinx.atomicfu.locks

import kotlinx.cinterop.*
import platform.posix.*
import kotlin.concurrent.*

public actual class NativeMutexNode {

@Volatile
private var isLocked = false
private val pMutex = nativeHeap.alloc<pthread_mutex_t>().apply { pthread_mutex_init(ptr, null) }
private val pCond = nativeHeap.alloc<pthread_cond_t>().apply { pthread_cond_init(ptr, null) }

internal actual var next: NativeMutexNode? = null

actual fun lock() {
pthread_mutex_lock(pMutex.ptr)
while (isLocked) { // wait till locked are available
pthread_cond_wait(pCond.ptr, pMutex.ptr)
}
isLocked = true
pthread_mutex_unlock(pMutex.ptr)
}

actual fun unlock() {
pthread_mutex_lock(pMutex.ptr)
isLocked = false
pthread_cond_broadcast(pCond.ptr)
pthread_mutex_unlock(pMutex.ptr)
}
}

0 comments on commit a70defe

Please sign in to comment.