9

The use of Locks and mutexes is illegal in hard real-time callbacks. Lock free variables can be read and written in different threads. In C, the language definition may or may not be broken, but most compilers spit out usable assembly code given that a variable is declared volatile (the reader thread treats the variable as as hardware register and thus actually issues load instructions before using the variable, which works well enough on most cache-coherent multiprocessor systems.)

Can this type of variable access be stated in Swift? Or does in-line assembly language or data cache flush/invalidate hints need to be added to the Swift language instead?

Added: Will the use of calls to OSMemoryBarrier() (from OSAtomic.h) before and after and each use or update of any potentially inter-thread variables (such as "lock-free" fifo/buffer status counters, etc.) in Swift enforce sufficiently ordered memory load and store instructions (even on ARM processors)?

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
  • Why should any high level language define a construct for a feature that only (seems) to work for a certain hardware architecture but can be compiled for many, including one with weakly ordered memory model (ARM)? Furthermore, the C `volatile` never works _reliable_ for what you are seeking for. – CouchDeveloper May 01 '16 at 14:35
  • 1
    One claim is that Swift is targeted to be useful as an industrial strength system programming language. Industrial systems are often connected to customer specific physical real-world inputs and controls which have real-time operational constraints. – hotpaw2 May 01 '16 at 16:30
  • In scenarios like these, you can still interface through a C API. But `volatile` will not be suitable for solving concurrent problems anyway. Did you take a look into the [Atomics operations library](http://en.cppreference.com/w/c/atomic) in the C11 standard? There are lock free primitives. – CouchDeveloper May 02 '16 at 09:53

1 Answers1

2

As you already mentioned, volatile only guarantees that the variable will not get cached into the registries (will get treated itself as a register). That alone does not make it lock free for reads and writes. It doesn't even guarantees it's atomicity, at least not in a consistent, cross-platform way.

Why? Instruction pipelining and oversizing (e.g using Float64 on a platform that has 32bit, or less, floating-point registers) first comes to mind.

That being said, did you considered using OSAtomic?

Rad'Val
  • 8,895
  • 9
  • 62
  • 92
  • There's plenty of stuff that is not cross-platform that still should be possible in a high level systems programming language for specific sets of systems. Platform specific should not have to mean "you must code in assembly language". The ARM manual specifies certain atomic write alignment and size requirements. – hotpaw2 May 01 '16 at 18:14
  • True, when developing for a certain processor you might want to take responsibility at higher levels and the programming language should support you. But I'm afraid Swift doesn't have an alternative for `volatile`, at least as far as I know. – Rad'Val May 01 '16 at 19:39