Drupal - how to define entity preprocess function

I created a general mymodule_preprocess(&$variables, $hook) function and it showed that the specific function name should be mymodule_preprocess_myentity. Where myentity is the proper name of the entity.

So, this code is working for me:

function mymodule_preprocess(&$variables, $hook) {
  if (isset($variables['elements']['#entity_type'])) { // or maybe check for $hook name
    $function = __FUNCTION__ . '_' . $variables['elements']['#entity_type'];
    if (function_exists($function)) {
      $function($variables, $hook);
    }
  }
}

function mymodule_preprocess_myentity(&$vars) {
  ...
}

A more generic approach:

/**
 * Implements hook_preprocess().
 */
function mymodule_preprocess(&$variables, $hook) {
  if (isset($variables['elements']['#entity_type'])) {
    $myhook = "preprocess_{$variables['elements']['#entity_type']}_{$variables['elements']['#bundle']}_{$variables['elements']['#view_mode']}";
    $modules = module_implements($myhook);

    foreach ($modules as $module) {
      $function = "{$module}_{$myhook}";
      $function($variables);
    }
  }
}

Unfortunately module_implements() doesn't check if the active theme implements the preprocess hook.