Monthly Archives: September 2015

Synchronization without Locking

<< Previous Article

We have covered the various synchronization mechanisms in the previous articles. One of the thing common among them was that they put the process to sleep, if the lock is not available. Also, all those are prone to deadlock, if not implemented carefully. Sometimes however, we require to protect a simple variable like integer. It can be as simple as setting a flag. Using semaphore or spinlock to protect such a variable may be overhead. So, does kernel provide any synchronization mechanism without locking? Read on to explore more on this.

Atomic Operations

Atomic operations are indivisible and uninterruptible. Each of these compile into a single machine instruction as far as possible and are guaranteed to be atomic. Kernel provides a atomic integer type atomic_t for atomic operations. Below are the operations:

#include <asm/atomic.h>

void atomic_set(atomic_t *a, int i); // Set the atomic variable a to integer value i
int atomic_read(atomic *a); // Return the value of atomic variable a
void atomic_add(int i, atomic_t *a); // Add i to atomic variable a
void atomic_sub(i, atomic_t *a); // Subtract i from atomic variable a
void atomic_inc(atomic_t *a); // Increment operation
void atomic_dec(atomic_t *a); // Decrement operation

Atomic Bit Operations

Many a times, the requirement is to flag some condition. For this, a single bit may serve the purpose well. However, atomic_t type variable doesn’t work well for manipulating the bits. For this, the kernel provides a set of operations as listed below:

#include <asm/bitops.h>

void set_bit(int nr, void *a); // Set the bit number nr in value pointed by a
void clear_bit(int nr, void *a); // Clear the bit number nr in value pointed by a
void change_bit(int nr, void *a); // Toggle the bit at position nr

Conclusion

So, these are the simple, yet powerful mechanisms to provide the synchronization without locking. These can be quite useful while dealing with integer and bit operations, respectively, and involve far less overhead as compared to the usual synchronization mechanisms such as semaphore and mutex. However, these might not be useful in achieving the critical sections.

With this, we are now familiar with most of the synchronization mechanisms provided by the kernel. As you understand, synchronization mechanisms come with its own pros and cons and we need to be very careful while selecting the right one.

Next Article >>

   Send article as PDF   

Multi-colour using RGB LED

This 8th article in the series of “Do It Yourself: Electronics”, discusses the multi colour generation using RGB LED.

<< Previous Article

Festival season was approaching fast. This time Pugs wanted to create some fancy lighting for the same to be decorated in his hostel room. First thought was to create some colourful lighting banner. Next thought was to possibly use the electronics, he has been learning. In electronics, lighting is synonymous to LEDs. But, then we are limited by colours – red, green, yellow, and blue – that is what Pugs used to think, before he explored further into LEDs. Upon exploration, he found out an orange LED, but more interestingly an RGB LED, where R stands for Red, G for Green, B for Blue. Yes, a single LED with three colours in it.

With his desire to create multi-colour lighting, that looked promising, as he has learnt from his computer graphics studies that with RGB, one can generate any colour. But that was computers, and this is electronics. So, what? Colours are colours. With all these thoughts ringing in his head, he walked towards his batch-mate & newly found electronics friend Vishal’s room.

Once at Vishal’s door, he knocked it. But no response. So, he pushed it, and it opened wide. Vishal was in his Krishna prayers. So, Pugs got inside and sat on the cot, waiting for Vishal to finish his prayers.

“What’s up Pugs? What brings you here?”, asked Vishal, after completing his prayers.

“Vishu! I can generate various colours using combination of RGB in computer graphics. Can I do similar things with RGB LEDs?”, queried Pugs.

“Yes! Of course, you can?”

“But how? In graphics, I have numbers from 0 to 255 for each of the colours, and I use different value combinations for different colours. How do I give that value in LEDs?”

“Think beyond numbers – what do they control?”

“Hmmm – intensity.”

“Exactly. So, here you control the intensity of the LED, by passing different currents through the LED.”

“Okay. But these RGB LEDs look so weird. I can do this for a single LED. But these RGB LEDs – some have 6 legs, but mostly have only 4 legs.”

“O! I see what’s your confusion. One with 6 legs looks fine to you, right?”

“Yes. 3 coloured LEDs in one. Each LED with 2 legs, and hence total of 6 legs.”

“But most of the time you don’t need all legs separate for them. You may control their intensity from the anode (+ve side), and the cathode (-ve side) could be common. Or, vice versa. And, in that case they would just need 4 legs.”

“Okay. Then, why do we have 6 leg LEDs.”

“Now, you are asking the other way round question – you can’t stop your questions.”

“That’s the way we learn, right?”

“Okay. Okay. Stop your gyaan. For example, if you want to connect the LEDs in series, it may not be possible with common cathode or common anode RGB LEDs.”

“Now, last question – how do I control the current through LEDs with the common terminal, say common cathode?”

“Same way, using variable resistors like potentiometers (pots) – just connect three – one with each colour’s anode.”

Concluding with thanks, Pugs rushes to his room to create a simple circuit to generate multi colours using his RGB LED(s). Some of the basis, which Pugs wants to try for his colour generation are as follows:

  • With Red + Green intensities – Brown, Orange, Yellow
  • With Green + Blue intensities – Cyan, Shades of Blue
  • With Blue + Red intensities – Pink, Magenta
  • With Red + Green + Blue intensities – White, Shades of Grey

What do you think? Will he be able to generate all these colours?

Next Article >>

   Send article as PDF