Check for table lock in conditional statement How can I make this work?

IF( EXISTS (SHOW OPEN TABLES WHERE In_use &lt; 2 and `Database` like 'MYDB' ) ) THEN
 -- DO SOMETHING
END IF


Context

I want to run some sql statements inside a procedure, only if the table x is not locked ( because of updates or any other reason ).

I want to do this on the MySQL server side as I have clients that trigger this procedure from more than 12 machines from bash, c and  c#.

So is there any way to get the table lock information in a conditional statement on the server side ?
The simplest solution to this seems like it would be to use a named lock. 

http://dev.mysql.com/doc/refman/5.5/en/miscellaneous-functions.html#function_get-lock

IF GET_LOCK('my_arbitrary_lock_name',0) THEN 
  ...
  DO RELEASE_LOCK('my_arbitrary_lock_name');
ELSE
  ...
END IF;


This pool of locks is independent of table locks, metadata locks, row locks, or any other kind of "actual" database object locks, and obtaining, releasing, or checking these locks is also independent of transactions.

The only thing that's actually getting "locked" here is the name you passed as the first argument, which is an arbitrary string.  All this does is ensure that no more than one thread can hold a lock of the same name at the same time.  The others have to wait for up to the number of seconds specified in the 2nd argument to GET_LOCK() otherwise they don't get the lock (and GET_LOCK() returns false).  After the timeout (which is 0, in the example, to wait no time at all) if the lock isn't free, the function returns false; if the lock is free then the lock is obtained by that connection and the function returns true.  Other connections can discover which connection holds a lock with the IS_USED_LOCK() function, but they can't interfere (other than kill the connection, given sufficient privileges).

If you "forget" to release a lock, it's automatically released if you disconnect.

Asking for a different lock will also release the current lock you hold in MySQL, and asking for a lock you hold will release it and then try to re-obtain it, potentially handing it over to another thread.  These behaviors vary between MySQL and some versions of MariaDB, it's best to be explicit when using them rather than relying on such side-effects for intentional behavior.