Wednesday, April 28, 2010

Solution for multithreaded GTK+ GUI hang and Random Crash.

Here is a solution that i found for multi-threaded PyGTK+ GUI hang/Crash. After a long search i found this code in some where on the internet, but i can't remember where is it really placed.

For What:
GTK+ is not thread safe, So we have to use "gtk.gdk.threads_enter" and "gtk.gdk.threads_leave" for that. This will make threading safe on GTK+, Like this
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Handling GTK+ GUI

but some time GUI hangs when same thread acquire this lock  for more than one time like this.
#Handling GTK+ GUI
gtk.gdk.threads_enter()  #************ 
#again Handling GTK+ GUI
So we have to find is this thread has it's lock already or not and make lock.
here is the code that will track threads and find is that really has it lock or not and make correct Locks.

class Check_For_Lock:
        def __init__(self):
                self.lock = threading.Lock()
                self.thread = None
                self.locked = 0
        def __enter__(self):
                with self.lock: DoLock = (thread.get_ident()!=self.thread)
                if DoLock:
                        with self.lock: self.thread = thread.get_ident()
                self.locked += 1
                return None

        def __exit__(self, exc_type, exc_value, traceback):
                with self.lock:
                        self.locked -= 1
                        if self.thread!=thread.get_ident():
                                print "!ERROR! Thread freenot locked lock!"
                                if self.locked == 0:
                                        self.thread = None
                                        #Added BY me  SEE WHY?:
                return None
TLocker = Check_For_Lock()

def TLocked(f):
    def wraps(f):return f
    def wrapper(*args, **kwds):
        with TLocker.lock:
            if TLocker.thread == None or TLocker.thread==thread.get_ident():
                TLocker.thread = thread.get_ident()
                TLocker.locked += 1
                WeHold = True
                print "***ERROR: GtkLocked for non-owned thread!"
                WeHold = False
        ret = f(*args, **kwds)
        if WeHold:
            with TLocker.lock:
                TLocker.locked -= 1
                if TLocker.locked == 0: TLocker.thread = None
        return ret
    return wrappr
I used this code for my project RoxBird Download Manager.
I got so many crashes on RoxBird. after that i removed gdk.threads_enter()/leave() and used this, Now RoxBird have a stable GUI.

you can use it simply like this

with TLocker:
    #Make changes on GTK+ GUI.
    window.set_size_request(400, 300)
#Connecting Signal via threads
widget.connect('signal_name', TLocked(CallFunction), UsrArg1, UsrArg2, ...)

  • You should create "gtk.gdk.threads_init()" after importing "gtk" module.  
  • You should use only one Instance of TLocker for the all application. (you can use reference for TLocker)
  • In threads all GUI handling should be inside of the "with TLocker" block.
  • "widget.connect" should called with TLocked like above, and connect statement should not inside of the "with TLocker" 
if i miss something on code or notes, feel free to comment.

Sunday, April 18, 2010

Tutorial to Flash/Upgrade Nokia mobile (You can do it yourself)

If you don't know what is Flashing:

Flashing or Upgrading your mobile is just like installing OS in your PC. But
the only one difference is, in PC processor was designed to work with many OS'
software's, but mobiles are not like PC. That can only work with it's own

Installing OS on PC can be done by connecting CD/DVD drive or Thumpdrive
with PC. Like that mobile Flashing can be done by connecting Mobile with PC via

For PC you need Setup Package of OS, Like PC mobile need its setup files,
There is a software named Navifirm to obtain these files for
us. you can get it from here: Download Navifirm

PC os's have a small software that can copy all needed files to HardDrive
while installing. mobile OS does not have softwares like PC OS's, So we need
another software to do that thing. In this example we are using JAF . You can
get it from here: Download JAF

You need:

You can get flash files on the internet. just google for "FLASH FILE (NOKIA
BB-5)" you'll get what you want.


Use Navifirm (see above for download).

How to use Navifirm:

First of all you need to know your mobile RM number and SW version. see
underneath the battery.

Navifirm Download


Type: RM-84
CODE: 0539276
Model: N70-1

after this open Navifirm then select like this: Your RM number > SW
version > any language/color you want

then download only *.mcusw, *.ppm and Image files.

after that rename *.mcusw file to *.c0r.

Now we have Flash files for selected mobile, and JAF installed on system.


Note: Don't miss any steps below. READ CAREFULLY****

◆ Disable and Uninstall PC suite if you have. and also Uninstall Nokia Software Updater (NSU).

◆ Open JAF (see above for download.)

◆ Clike on tab what mobile you have (like BB5 or WD2) see/know more:BB5

◆ Connect phone in "PC suite" mode.

◆ Tick manual flash, factory set and normal mode. Also CRT 308 but it's already ticked.

◆ If you're flashing a DEAD phone, tick the manual flash, Factory set, Normal mode and dead flash. And remove tick from CRT308

◆ select the
MCU (the renamed **.c0r file),
CNT (the **.image file)

◆ Plug in the phone to charger.

In case of dead flashing, don't connect the charger yet.

◆ Check cable connection for stable and then HIT "FLASH" .

◆Hit "OK"

In case of dead flashing, When jaf says "PRESS POWER ON NOW" , connect the charger and turn on the phone.

◆ the screen should go white and stay that way.

◆ Don't touch phone or cable till JAF says "done".

You may need to WAIT upto 10min for JAF finish.

Disclaimer:You should know some basic ideas about what you doing. I am not responsible for any loses due to wrong process, incorrect hardware's,careless.